JAVA基础50题(1-6)

 【程序1】 

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问2年内每个月的兔子总数为多少? 

分析:

可把兔子分类,即新生的兔子,出生两个月的兔子(下个月即能开始繁殖的),成年兔子(开始繁殖)。

新生  两个月  成兔      总数

1         0           0           1

0         1           0           1

1         0           1           2

1         1           1           3

2         1           2           5

3         2           3           8

……

可以看到结果是Fibonacci数列。

Code:
  1. /** 
  2.  * @author Geek_Soledad 
  3.  * 
  4.  */  
  5. public class Fibonacci {  
  6.     public static void main ( String []args) {  
  7.         int f1 = 1;  
  8.         int f2 = 1;  
  9.         for ( int i = 1; i <= 24 ; i++) {  
  10.             System.out.println("第" + i + "个月有" + f1 + "只兔子。");  
  11.             f2 +=f1;  
  12.             f1 = f2 - f1;  
  13.         }  
  14.     }  
  15. }  

 

【程序2

题目:判断101-200之间有多少个素数,并输出所有素数。 

Code:
  1. /** 
  2.  * @author Geek_Soledad 
  3.  * 
  4.  */  
  5. public class Primes {  
  6.     public static void main ( String []args) {  
  7.         int num = 0;  
  8.         for ( int n = 101; n < 200; n += 2) {  
  9.             if (isPrimes( n)) {  
  10.                 System.out.print( " " + n);  
  11.                 num++;  
  12.             }  
  13.         }  
  14.         System.out.println("共有素数" + num + "个");  
  15.     }  
  16.     static boolean isPrimes ( int n) {  
  17.         for ( int i = 3; i < n; i += 2) {  
  18.             if ( n % i == 0) {  
  19.                 return false;  
  20.             }  
  21.         }  
  22.         return true;  
  23.     }  
  24. }  

【程序3】 

题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。 

Code:
  1. /**  
  2.  * @author Geek_Soledad  
  3.  * @creation date 2011-4-4 上午10:27:26  
  4.  *  
  5.  */  
  6.   
  7. public class DaffodilNum {  
  8.     public static void main ( String []args) {  
  9.         int unit;  
  10.         int decade;  
  11.         int hundreds;  
  12.         for ( int n = 100; n < 1000; n++ ) {  
  13.             unit = n % 10;  
  14.             decade = n /10 % 10;  
  15.             hundreds = n / 100;  
  16.             if ( Math.pow(unit, 3) + Math.pow(decade, 3)   
  17.                     + Math.pow(hundreds, 3) == n) {  
  18.                 System.out.print(n + " ");  
  19.             }  
  20.         }  
  21.     }  
  22. }  

【程序4】 

题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 

Code:
  1. /**  
  2.  * @author Geek_Soledad  
  3.  * @creation date 2011-4-4 上午10:27:26  
  4.  *  
  5.  */  
  6.   
  7. public class Factoring {  
  8.     public static void main( String []args) {  
  9.         int num = 0;  
  10.         if ( args.length < 1) {  
  11.             System.out.println("usage: java Factoring number");  
  12.             System.out.println("     The number must be positive integer type");  
  13.         } else {  
  14.             try {  
  15.                 num = Integer.parseInt(args[0]);  
  16.                 if ( num <= 0) {  
  17.                     System.out.println("The number must be positive integer type");  
  18.                 }  
  19.                 System.out.print( num + "=");  
  20.                 factoring( num, 2);  
  21.             } catch( NumberFormatException nfe) {  
  22.                 System.out.println("The number must be positive integer type");  
  23.             }  
  24.         }  
  25.   
  26.     }  
  27.     static void factoring( int n, int factor) {  
  28.         if ( n == factor) {  
  29.             System.out.println(factor);  
  30.         } else if ( n % factor == 0) {  
  31.             System.out.print(factor + "*");  
  32.             factoring( n/factor, factor);  
  33.         } else {  
  34.             factoring( n, factor + 1);  
  35.         }  
  36.     }  
  37. }  

【程序5】 

题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。 

Code:
  1. /**  
  2.  * @author Geek_Soledad  
  3.  * @creation date 2011-4-4 上午11:10:10  
  4.  *  
  5.  */  
  6.   
  7. public class Score {  
  8.     public static void main ( String []args) {  
  9.         int score;  
  10.         if ( args.length < 1) {  
  11.             System.out.println("usage: " + new Score().getClass() + " n");  
  12.         } else {  
  13.             try {  
  14.                 score = Integer.parseInt(args[0]);  
  15.                 if ( score >= 90) {  
  16.                     System.out.println( "A");  
  17.                 } else if ( score >= 60) {  
  18.                     System.out.println( "B");  
  19.                 } else {  
  20.                     System.out.println( "C");  
  21.                 }  
  22.             } catch( NumberFormatException nfe) {  
  23.                 System.out.println("The number must be positive integer type");  
  24.             }  
  25.         }  
  26.     }  
  27. }  

 【程序6】 

题目:输入两个正整数mn,求其最大公约数和最小公倍数。

Code:
  1. /**  
  2.  * @author Geek_Soledad  
  3.  * @creation date 2011-4-4 下午01:46:47  
  4.  *  
  5.  */  
  6.   
  7. public class Test**LCM {  
  8.     public static void main ( String []args) {  
  9.         int m = 40;  
  10.         int n = 60;  
  11.         int hcfNum ,lcmNum;  
  12.         hcfNum = hcf( m, n);  
  13.         if ( hcfNum < 0) {  
  14.             System.out.println("请输入两个正整数");  
  15.         }  
  16.         System.out.println("The HCD is " + hcfNum);  
  17.         lcmNum = lcm ( m, n, hcfNum);  
  18.         System.out.println("The LCM is " + lcmNum);  
  19.     }  
  20.       
  21.     /*本方法采用欧几德里算法。*/  
  22.     /* 发了帖才发现最大公约数的另一个缩写不是你想写就能写的,要和谐*/  
  23.     static int hcf( int m, int n) {  
  24.         int tmp;  
  25.         if ( m <= 0 || n <= 0) {  
  26.             return -1;  
  27.         }  
  28.         if ( m < n) {  
  29.             tmp = m;  
  30.             m = n;  
  31.             n = tmp;  
  32.         }  
  33.         while ( n > 0) {  
  34.             tmp = m % n;  
  35.             m = n;  
  36.             n = tmp;  
  37.         }  
  38.         return m;  
  39.     }  
  40.       
  41.     static int lcm(int m, int n, int hcf) {  
  42.         return m * n / hcf;  
  43.     }  
  44. }  

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值