java经典50题

时间:2016-04-04 05:46:56
【文件属性】:
文件名称:java经典50题
文件大小:103KB
文件格式:DOC
更新时间:2016-04-04 05:46:56
java 比较经典的java编程习题 【程序1】   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?   //这是一个菲波拉契数列问题public class lianxi01 {public static void main(String[] args) {System.out.println("第1个月的兔子对数:    1");System.out.println("第2个月的兔子对数:    1");int f1 = 1, f2 = 1, f, M=24;     for(int i=3; i<=M; i++) {      f = f2;      f2 = f1 + f2;      f1 = f;      System.out.println("第" + i +"个月的兔子对数: "+f2);         }}} 【程序2】   题目:判断101-200之间有多少个素数,并输出所有素数。 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。   public class lianxi02 {public static void main(String[] args) {    int count = 0;    for(int i=101; i<200; i+=2) {     boolean b = false;     for(int j=2; j<=Math.sqrt(i); j++)      {        if(i % j == 0) { b = false; break; }          else           { b = true; }     }        if(b == true) {count ++;System.out.println(i );}                                  }    System.out.println( "素数个数是: " + count);}}

网友评论