tzkr.net
当前位置:首页 >> jAvA使用递归计算100 >>

jAvA使用递归计算100

public class test { public static int sum(int s){ if(s==0) return 0; else return s+sum(--s); //递归体现在这里 } public static void main(string[] args) { system.out.println(sum(100));//打印计算的结果 } }

public class test { /*创建类*/public static void main(string[] args) {system.out.println(dg(100));}static int dg(int i) { /*定义变量 */int sum;if (i == 1) /*假设条件*/return 1;elsesum = i + dg(i - 1); /*1~100的和的表达式*/return sum; /*返回结果*/}} 递归是计算

希望可以帮到你!public class SUM{public static void main(String[] args) {Scanner s=new Scanner(System.in); System.out.println("请输入n: "); int n=s.nextInt(); int sum = sdg(n); System.out.println(sum); } static int sdg(int x){ if(x==1){return 1;} else{return x+sdg(x-1); }}

class test { public static void main(String[]args) System.out.println(Sum(2)); } public static int Sum(int m) { if(m==100) return 100; else return m+Sum(m+2); } }

可以倒传参数,从100开始,直到1、加完就结束~~~~~

第一题:public class CalSum { public static void main(String[] args) { CalSum calSum = new CalSum(); int result = calSum.calculate(100); System.out.println("1+2+3++100="+result); } public int calculate(int number) { int result = 0; if(number == 1

public class dgqh { public static void main(string[] args) { // todo auto-generated method stub int sum = sdg(100); system.out.println(sum); } static int sdg(int x) { if(x==1) { return 1; } else{ return x+sdg(x-1); } } }

一楼你 + 1/2*(n-1); //递归条件这里是0 所以是得不到正确答案的..public class fe{ public static void main(String[] args) { double sum = 1; for (int i = 1; i 评论0 0 0

public class MyMain { public static void main(String[] args) { BigInteger n = new BigInteger( "1"); for(int i=1;i<=200;i++) { n=n.multiply(new BigInteger(String.valueOf(i))); } System.out.println(n); //这就是32313133353236313431303231363533e

public class test { public static void main(string args[]) { int x = 5; int rs = fac(x); system.out.println("" + x + "! = " + rs); } public static long fac(int x) { /*阶乘算法*/ if (x > 1) return (x * fac(x - 1)); /*递归*/ else return 1; }}

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