[size=10.5000pt]public class Prog1{ [size=10.5000pt] [size=10.5000pt]public static void main(String[] args){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]int n = 10; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]System.out.println("第"+n+"个月兔子总数为"+fun(n)); [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt]private static int fun(int n){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]if(n==1 || n==2) [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] return 1; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]else [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] return fun(n-1)+fun(n-2); [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt]} |
[size=10.5000pt]public class Prog2{ [size=10.5000pt] [size=10.5000pt]public static void main(String[] args){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]int m = 1; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]int n = 1000; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]int count = 0; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]//统计素数个数 [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]for(int i=m;i<n;i++){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]if(isPrime(i)){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]count++; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]System.out.print(i+" "); [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]if(count%10==0){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]System.out.println(); [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]System.out.println(); [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]System.out.println("在"+m+"和"+n+"之间共有"+count+"个素数"); [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt]//判断素数 [size=10.5000pt] [size=10.5000pt]private static boolean isPrime(int n){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]boolean flag = true; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]if(n==1) [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] flag = false; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]else{ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]for(int i=2;i<=Math.sqrt(n);i++){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]if((n%i)==0 || n==1){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]flag = false; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]break; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] else [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] flag = true; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] } [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]return flag; [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt]} |
[size=10.5000pt]public class Prog3{ [size=10.5000pt] [size=10.5000pt]public static void main(String[] args){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]for(int i=100;i<1000;i++){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]if(isLotus(i)) [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] System.out.print(i+" "); [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]System.out.println(); [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt]//判断水仙花数 [size=10.5000pt] [size=10.5000pt]private static boolean isLotus(int lotus){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]int m = 0; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]int n = lotus; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]int sum = 0; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]m = n/100; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]n -= m*100; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]sum = m*m*m; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]m = n/10; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]n -= m*10; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]sum += m*m*m + n*n*n; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]if(sum==lotus) [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]return true; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]else [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]return false; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt]} |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |