rpct.net
当前位置:首页 >> JAVA:请使用递归实现1到1000的整数累加,并把结果输出到控制台 >>

JAVA:请使用递归实现1到1000的整数累加,并把结果输出到控制台

int sum(int n) { if(n == 1) return 1; else return sum(n - 1) + n; }

public class Count { public static void main(String[] args) throws IOException { Scanner scanner=new Scanner(System.in); Integer i=scanner.nextInt(); for (int j = 0; j <20 ; j++) { i=i+i; System.out.println("第"+j+"次累加:"+i); } } }

public class Test { long f(int n){ if(n ==1){ return 1; } return n+f(n-1) ; } public static void main(String args[]){ System.out.print(new Test().f(1000));// N } }

C语言 main() { int sum,i; sum=0; for(i=1;i { if(i%2==0) sum=sum+i; } printf("%d",sum); }

使用数组的递归:public class Test12 { static int M = 4; static int N = 3; static int[] a= new int[]{1,2,3,4}; static int[] b = new int[N]; public static void main(String[] args){ C(M,N); } static void C(int m,int n){ int i,j; for(i=n;i<=m;i++) { b[n-1] = i-1; if(n>1) C(i-

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println(f(n)); } public static int f(int n) { if (n == 1) return 1; else { return n + f(n - 1); } } }

public static void main(string[] args) { for(int i=0;i 评论0 0 0

class sum+12345{ public static void main() { int sum=0; for ( int i = 0 ; i 评论0 0 0

public class MathRandom { public static void main(String[] args){ for(int i = 0;i < 10;i++){ int randomnum = (int)(Math.random()*9000+1000); System.out.println(randomnum); } } }

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