kgdc.net
当前位置:首页 >> 用递归函数实现一个累加和函数sum()计算1+2+3+....... >>

用递归函数实现一个累加和函数sum()计算1+2+3+.......

#include "stdio.h"int sum(int n){ if(n==1) return 1; return n+sum(n-1);}int main(void){ int k; while(1){ printf("Input k(int 0

#include int Func(int); int Sum(int); int main () { int i,n; printf("请输入n值:"); scanf("%d",&n); printf("1!+2!+...+n!=%d\n",Sum(n)); return 0; } int Func(int n) //递归求n! { int sum=1; if(n==1 || n==0) return 1; else sum=n*F...

Java程序: public class Main {public static void main(String[] args){System.out.println(sum(2, 5));}public static int sum(int n1, int n2) {if(n1 == n2) {return n1;}if(n1 > n2) {int temp = n1;n1 = n2;n2 = temp;}return sum(n1, n2...

这个题很简单 public class computer { public static void main(String args[]){ int sum=ADD(100); System.out.println(sum); } public static int ADD(int num){ if(num==1) return 1; else return ADD(num-1)+num; } }

long sum(long n){ if(n==0) return 0; else if(n==1) return 2; else if(n>1) return n*(n+1)+sum(n-1); else return -1;}

是递归, 没有递推一说, 只有递归和循环两种, 或者直接计算,即知道其数学公式, #include #include int sum(int n) { if (n == 1) return n; else return sum(n - 1) + n; } int sum1(int n) { return n * (n + 1) / 2; } int sum2(int n) { int s...

#include double fac(int num)// 因为1/n不是整数,所以这里和下面求和的部分都要用double { double sum; if(num

#include "stdio.h" int sum(int n) { if (n==1||n==2) return n; else return n+sum(n-2); } void main() { int n; printf("请输入n的值: \n"); scanf("%d",&n); while (n

#include void main() { int f(int i); int sum,n; printf("输入n:"); scanf("%d",&n); sum=f(n); printf("sum=%d",sum); } int f(int i) { if(i==1) return 1; else return f(i-1)+i; return 0; } 正宗的递归函数哦!!

#include int fun(int n) { int sum = 0; if (n == 1) sum = n; else sum = n + fun (n-1); return sum; } void main() { int n; scanf("%d",&n); printf("%d",fun(n)); }

网站首页 | 网站地图
All rights reserved Powered by www.kgdc.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com