JAVA经典算法40题

  1. 【程序1】       题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?         
  2. 1.程序分析:       兔子的规律为数列1,1,2,3,5,8,13,21....         
  3. public   class   exp2{   
  4. public   static   void   main(String   args[]){   
  5. int   i=0;   
  6. for(i=1;i <=20;i++)   
  7. System.out.println(f(i));   
  8. }   
  9. public   static   int   f(int   x)   
  10. {   
  11. if(x==1   ||   x==2)   
  12. return   1;   
  13. else   
  14. return   f(x-1)+f(x-2);   
  15. }   
  16. }   
  17. 或   
  18. public   class   exp2{   
  19. public   static   void   main(String   args[]){   
  20. int   i=0;   
  21. math   mymath   =   new   math();   
  22. for(i=1;i <=20;i++)   
  23. System.out.println(mymath.f(i));   
  24. }   
  25.   
  26. }   
  27. class   math   
  28. {   
  29. public   int   f(int   x)   
  30. {   
  31. if(x==1   ||   x==2)   
  32. return   1;   
  33. else   
  34. return   f(x-1)+f(x-2);   
  35. }   
  36. }   
  37.   
  38. 【程序2】       题目:判断101-200之间有多少个素数,并输出所有素数。         
  39. 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,         
  40. 则表明此数不是素数,反之是素数。         
  41. public   class   exp2{   
  42. public   static   void   main(String   args[]){   
  43. int   i=0;   
  44. math   mymath   =   new   math();   
  45. for(i=2;i <=200;i++)   
  46. if(mymath.iszhishu(i)==true)   
  47. System.out.println(i);   
  48. }   
  49. }   
  50. class   math   
  51. {   
  52. public   int   f(int   x)   
  53. {   
  54. if(x==1   ||   x==2)   
  55. return   1;   
  56. else   
  57. return   f(x-1)+f(x-2);   
  58. }   
  59. public   boolean   iszhishu(int   x)   
  60. {   
  61. for(int   i=2;i <=x/2;i++)   
  62. if   (x   %   2==0   )   
  63. return   false;   
  64. return   true;   
  65. }   
  66. }   
  67.   
  68. 【程序3】       题目:打印出所有的   "水仙花数   ",所谓   "水仙花数   "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个   "水仙花数   ",因为153=1的三次方+5的三次方+3的三次方。         
  69. 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。         
  70. public   class   exp2{   
  71. public   static   void   main(String   args[]){   
  72. int   i=0;   
  73. math   mymath   =   new   math();   
  74. for(i=100;i <=999;i++)   
  75. if(mymath.shuixianhua(i)==true)   
  76. System.out.println(i);   
  77. }   
  78. }   
  79. class   math   
  80. {   
  81. public   int   f(int   x)   
  82. {   
  83. if(x==1   ||   x==2)   
  84. return   1;   
  85. else   
  86. return   f(x-1)+f(x-2);   
  87. }   
  88. public   boolean   iszhishu(int   x)   
  89. {   
  90. for(int   i=2;i <=x/2;i++)   
  91. if   (x   %   2==0   )   
  92. return   false;   
  93. return   true;   
  94. }   
  95. public   boolean   shuixianhua(int   x)   
  96. {   
  97.       int   i=0,j=0,k=0;   
  98.       i=x   /   100;   
  99.       j=(x   %   100)   /10;   
  100.       k=x   %   10;   
  101.       if(x==i*i*i+j*j*j+k*k*k)   
  102.       return   true;   
  103.       else   
  104.       return   false;   
  105.         
  106. }   
  107. }   
  108. 【程序4】       题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。         
  109. 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:         
  110. (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。         
  111. (2)如果n   <>   k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你,重复执行第一步。         
  112. (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。         
  113. public   class   exp2{   
  114. public   exp2(){}   
  115.         public   void   fengjie(int   n){   
  116.                 for(int   i=2;i <=n/2;i++){   
  117.                         if(n%i==0){   
  118.                                 System.out.print(i+ "* ");   
  119.                                 fengjie(n/i);   
  120.                                 }   
  121.                 }   
  122.                 System.out.print(n);   
  123.                 System.exit(0);///不能少这句,否则结果会出错   
  124.                 }   
  125.                 public   static   void   main(String[]   args){   
  126.                           String   str= " ";   
  127.                           exp2   c=new   exp2();   
  128.                           str=javax.swing.JOptionPane.showInputDialog( "请输入N的值(输入exit退出): ");   
  129.                           int   N;   
  130.                           N=0;   
  131.                           try{   
  132.                                           N=Integer.parseInt(str);   
  133.                                           }catch(NumberFormatException   e){   
  134.                                                   e.printStackTrace();   
  135.                                                   }   
  136.                         System.out.print(N+ "分解质因数: "+N+ "= ");   
  137.                         c.fengjie(N);   
  138.                 }           
  139. }   
  140. 【程序5】       题目:利用条件运算符的嵌套来完成此题:学习成绩>   =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。         
  141. 1.程序分析:(a>   b)?a:b这是条件运算符的基本例子。         
  142. import   javax.swing.*;   
  143. public   class   ex5   {   
  144.                 public   static   void   main(String[]   args){   
  145.                           String   str= " ";   
  146.                           str=JOptionPane.showInputDialog( "请输入N的值(输入exit退出): ");   
  147.                           int   N;   
  148.                           N=0;   
  149.                           try{   
  150.                                 N=Integer.parseInt(str);   
  151.                             }   
  152.                           catch(NumberFormatException   e){   
  153.                                 e.printStackTrace();   
  154.                               }   
  155.                           str=(N> 90"A ":(N> 60"B ""C "));   
  156.                           System.out.println(str);   
  157.                 }           
  158. }   
  159. 【程序6】       题目:输入两个正整数m和n,求其最大公约数和最小公倍数。         
  160. 1.程序分析:利用辗除法。         
  161. 最大公约数:   
  162. public   class   CommonDivisor{   
  163.         public   static   void   main(String   args[])   
  164.         {   
  165.                 commonDivisor(24,32);   
  166.         }   
  167.         static   int   commonDivisor(int   M,   int   N)   
  168.         {   
  169.                 if(N <0||M <0)   
  170.                 {   
  171.                         System.out.println( "ERROR! ");   
  172.                         return   -1;   
  173.                 }   
  174.                 if(N==0)   
  175.                 {   
  176.                         System.out.println( "the   biggest   common   divisor   is   : "+M);   
  177.                         return   M;   
  178.                 }   
  179.                 return   commonDivisor(N,M%N);   
  180.         }   
  181. }   
  182. 最小公倍数和最大公约数:   
  183. import   java.util.Scanner;     
  184. public   class   CandC     
  185. {     
  186. //下面的方法是求出最大公约数   
  187. public   static   int   gcd(int   m,   int   n)     
  188. {     
  189. while   (true)     
  190. {     
  191. if   ((m   =   m   %   n)   ==   0)     
  192. return   n;     
  193. if   ((n   =   n   %   m)   ==   0)     
  194. return   m;     
  195. }     
  196. }     
  197. public   static   void   main(String   args[])   throws   Exception     
  198. {     
  199. //取得输入值   
  200. //Scanner   chin   =   new   Scanner(System.in);     
  201. //int   a   =   chin.nextInt(),   b   =   chin.nextInt();     
  202. int   a=23;   int   b=32;   
  203. int   c   =   gcd(a,   b);     
  204. System.out.println( "最小公倍数: "   +   a   *   b   /   c   +   "\n最大公约数: "   +   c);     
  205. }     
  206. }   
  207. 【程序7】       题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。         
  208. 1.程序分析:利用while语句,条件为输入的字符不为   '\n   '.         
  209. import   java.util.Scanner;   
  210. public   class   ex7   {   
  211.   public   static   void   main(String   args[])   
  212.   {   
  213.     System.out.println( "请输入字符串: ");   
  214.     Scanner   scan=new   Scanner(System.in);   
  215.     String   str=scan.next();   
  216.     String   E1= "[\u4e00-\u9fa5] ";   
  217.     String   E2= "[a-zA-Z] ";   
  218.     int   countH=0;   
  219.     int   countE=0;   
  220.     char[]   arrChar=str.toCharArray();   
  221.     String[]   arrStr=new   String[arrChar.length];   
  222.     for   (int   i=0;i <arrChar.length   ;i++   )   
  223.     {   
  224.       arrStr[i]=String.valueOf(arrChar[i]);   
  225.     }   
  226.     for   (String   i:   arrStr   )   
  227.     {   
  228.       if   (i.matches(E1))   
  229.       {   
  230.         countH++;   
  231.       }   
  232.       if   (i.matches(E2))   
  233.       {   
  234.         countE++;   
  235.       }   
  236.     }   
  237.     System.out.println( "汉字的个数 "+countH);   
  238.     System.out.println( "字母的个数 "+countE);   
  239.   }   
  240. }     
  241. 【程序8】       题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。         
  242. 1.程序分析:关键是计算出每一项的值。         
  243. import   java.io.*;   
  244. public   class   Sumloop   {   
  245.     public   static   void   main(String[]   args)   throws   IOException   
  246.     {   
  247.     int   s=0;   
  248.     String   output= " ";   
  249.     BufferedReader   stadin   =   new   BufferedReader(new   InputStreamReader(System.in));   
  250.     System.out.println( "请输入a的值 ");   
  251.     String   input   =stadin.readLine();   
  252.     for(int   i   =1;i <=Integer.parseInt(input);i++)   
  253.     {   
  254.     output+=input;   
  255.     int   a=Integer.parseInt(output);   
  256.     s+=a;   
  257.     }   
  258.     System.out.println(s);   
  259.     }   
  260. }   
  261. 另解:   
  262. import   java.io.*;   
  263. public   class   Sumloop   {   
  264.     public   static   void   main(String[]   args)   throws   IOException   
  265.     {   
  266.     int   s=0;   
  267.     int   n;   
  268.     int   t=0;   
  269.     BufferedReader   stadin   =   new   BufferedReader(new   InputStreamReader(System.in));   
  270.     String   input   =   stadin.readLine();   
  271.     n=Integer.parseInt(input);   
  272.     for(int   i=1;i <=n;i++){   
  273.       t=t*10+n;   
  274.       s=s+t;   
  275.       System.out.println(t);   
  276.     }   
  277.     System.out.println(s);   
  278.   }   
  279. }   
  280. 【程序9】       题目:一个数如果恰好等于它的因子之和,这个数就称为   "完数   "。例如6=123.编程       找出1000以内的所有完数。         
  281. public   class   Wanshu   {   
  282.   public   static   void   main(String[]   args)   
  283.   {   
  284.   int   s;   
  285.   for(int   i=1;i <=1000;i++)   
  286.   {   
  287.   s=0;   
  288.   for(int   j=1;j <i;j++)   
  289.   if(i   %   j==0)   
  290.   s=s+j;   
  291. if(s==i)   
  292. System.out.print(i+ "   ");   
  293.   }   
  294.   System.out.println();   
  295.   }   
  296. }   
  297. 【程序10】   题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在       第10次落地时,共经过多少米?第10次反弹多高?         
  298. public   class   Ex10   {   
  299.   public   static   void   main(String[]   args)   
  300.   {   
  301.   double   s=0;   
  302.   double   t=100;   
  303.   for(int   i=1;i <=10;i++)   
  304.   {   
  305.                   s+=t;   
  306.                   t=t/2;   
  307.   }   
  308.   System.out.println(s);   
  309.   System.out.println(t);   
  310.     
  311.   }   
  312. }   
Java代码   收藏代码
  1. 【程序11】   题目:有1234个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?     
  2. 1.程序分析:可填在百位、十位、个位的数字都是1234。组成所有的排列后再去   掉不满足条件的排列。     
  3. public class Wanshu {  
  4.  public static void main(String[] args)  
  5.  {  
  6.     int i=0;  
  7.     int j=0;  
  8.     int k=0;  
  9.     int t=0;  
  10.     for(i=1;i<=4;i++)  
  11.         for(j=1;j<=4;j++)  
  12.             for(k=1;k<=4;k++)  
  13.                 if(i!=j && j!=k && i!=k)  
  14.                 {t+=1;  
  15.                     System.out.println(i*100+j*10+k);  
  16.  }    
  17.     System.out.println (t);  
  18.     }  
  19. }  
  20. 【程序12】  题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?     
  21. 1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。     
  22. import java .util.*;   
  23. public class test {  
  24.     public static void main (String[]args){   
  25.         double sum;//声明要储存的变量应发的奖金   
  26.         Scanner input =new Scanner (System.in);//导入扫描器   
  27.         System.out.print ("输入当月利润");   
  28.         double lirun=input .nextDouble();//从控制台录入利润   
  29.         if(lirun<=100000){   
  30.             sum=lirun*0.1;   
  31.         }else if (lirun<=200000){   
  32.             sum=10000+lirun*0.075;   
  33.         }else if (lirun<=400000){   
  34.             sum=17500+lirun*0.05;   
  35.         }else if (lirun<=600000){   
  36.             sum=lirun*0.03;   
  37.         }else if (lirun<=1000000){   
  38.             sum=lirun*0.015;   
  39.         } else{   
  40.             sum=lirun*0.01;   
  41.         }   
  42.         System.out.println("应发的奖金是"+sum);   
  43.         }   
  44. }  
  45. 后面其他情况的代码可以由读者自行完善.  
  46.   
  47. 【程序13】     
  48. 题目:一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?     
  49. 1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:     
  50. public class test {  
  51.     public static void main (String[]args){   
  52.     long k=0;  
  53.     for(k=1;k<=100000l;k++)  
  54.         if(Math.floor(Math.sqrt(k+100))==Math.sqrt(k+100) && Math.floor(Math.sqrt(k+168))==Math.sqrt(k+168))  
  55.             System.out.println(k);  
  56.     }  
  57. }  
  58. 【程序14】 题目:输入某年某月某日,判断这一天是这一年的第几天?     
  59. 1.程序分析:以35日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。     
  60. import java.util.*;  
  61. public class test {  
  62.     public static void main (String[]args){   
  63.         int day=0;  
  64.         int month=0;  
  65.         int year=0;  
  66.         int sum=0;  
  67.         int leap;     
  68.         System.out.print("请输入年,月,日\n");     
  69.         Scanner input = new Scanner(System.in);  
  70.         year=input.nextInt();  
  71.         month=input.nextInt();  
  72.         day=input.nextInt();  
  73.         switch(month) /*先计算某月以前月份的总天数*/    
  74.         {     
  75.         case 1:  
  76.             sum=0;break;     
  77.         case 2:  
  78.             sum=31;break;     
  79.         case 3:  
  80.             sum=59;break;     
  81.         case 4:  
  82.             sum=90;break;     
  83.         case 5:  
  84.             sum=120;break;     
  85.         case 6:  
  86.             sum=151;break;     
  87.         case 7:  
  88.             sum=181;break;     
  89.         case 8:  
  90.             sum=212;break;     
  91.         case 9:  
  92.             sum=243;break;     
  93.         case 10:  
  94.             sum=273;break;     
  95.         case 11:  
  96.             sum=304;break;     
  97.         case 12:  
  98.             sum=334;break;     
  99.         default:  
  100.             System.out.println("data error");break;  
  101.         }     
  102.         sum=sum+day; /*再加上某天的天数*/    
  103.         if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/    
  104.             leap=1;     
  105.         else    
  106.             leap=0;     
  107.         if(leap==1 && month>2)/*如果是闰年且月份大于2,总天数应该加一天*/    
  108.             sum++;     
  109.         System.out.println("It is the the day:"+sum);  
  110.         }  
  111. }  
  112. 【程序15】 题目:输入三个整数x,y,z,请把这三个数由小到大输出。     
  113. 1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x> y则将x与y的值进行交换,然后再用x与z进行比较,如果x> z则将x与z的值进行交换,这样能使x最小。     
  114. import java.util.*;  
  115. public class test {  
  116.     public static void main (String[]args){   
  117.         int i=0;  
  118.         int j=0;  
  119.         int k=0;  
  120.         int x=0;  
  121.         System.out.print("请输入三个数\n");     
  122.         Scanner input = new Scanner(System.in);  
  123.         i=input.nextInt();  
  124.         j=input.nextInt();  
  125.         k=input.nextInt();  
  126.         if(i>j)  
  127.         {  
  128.           x=i;  
  129.           i=j;  
  130.           j=x;  
  131.         }  
  132.         if(i>k)  
  133.         {  
  134.           x=i;  
  135.           i=k;  
  136.           k=x;  
  137.         }  
  138.         if(j>k)  
  139.         {  
  140.           x=j;  
  141.           j=k;  
  142.           k=x;  
  143.         }  
  144.         System.out.println(i+", "+j+", "+k);  
  145.     }  
  146. }  
  147. 【程序16】 题目:输出9*9口诀。     
  148. 1.程序分析:分行与列考虑,共99列,i控制行,j控制列。     
  149. public class jiujiu {  
  150. public static void main(String[] args)  
  151. {  
  152.     int i=0;  
  153.     int j=0;  
  154.     for(i=1;i<=9;i++)  
  155.     {    for(j=1;j<=9;j++)  
  156.             System.out.print(i+"*"+j+"="+i*j+"\t");  
  157.             System.out.println();  
  158.     }  
  159. }  
  160. }  
  161. 不出现重复的乘积(下三角)  
  162. public class jiujiu {  
  163. public static void main(String[] args)  
  164. {  
  165.     int i=0;  
  166.     int j=0;  
  167.     for(i=1;i<=9;i++)  
  168.     {    for(j=1;j<=i;j++)  
  169.             System.out.print(i+"*"+j+"="+i*j+"\t");  
  170.             System.out.println();  
  171.     }  
  172. }  
  173. }  
  174. 上三角  
  175. public class jiujiu {  
  176. public static void main(String[] args)  
  177. {  
  178.     int i=0;  
  179.     int j=0;  
  180.     for(i=1;i<=9;i++)  
  181.     {    for(j=i;j<=9;j++)  
  182.             System.out.print(i+"*"+j+"="+i*j+"\t");  
  183.             System.out.println();  
  184.     }  
  185. }  
  186. }  
  187. 【程序17】   题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。     
  188. 1.程序分析:采取逆向思维的方法,从后往前推断。     
  189. public class 猴子吃桃 {  
  190.     static int total(int day){  
  191.          if(day == 10){  
  192.           return 1;  
  193.          }  
  194.          else{  
  195.           return (total(day+1)+1)*2;  
  196.          }  
  197.         }  
  198. public static void main(String[] args)  
  199. {  
  200.     System.out.println(total(1));  
  201. }  
  202. }  
  203. 【程序18】   题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。     
  204. 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,   则表明此数不是素数,反之是素数。     
  205. import java.util.ArrayList;  
  206. public class pingpang {  
  207.      String a,b,c;  
  208.      public static void main(String[] args) {  
  209.       String[] op = { "x""y""z" };  
  210.       ArrayList<pingpang> arrayList=new ArrayList<pingpang>();  
  211.       for (int i = 0; i < 3; i++)  
  212.        for (int j = 0; j < 3; j++)  
  213.         for (int k = 0; k < 3; k++) {  
  214.             pingpang a=new pingpang(op[i],op[j],op[k]);  
  215.          if(!a.a.equals(a.b)&&!a.b.equals(a.c)&&!a.a.equals("x")  
  216.            &&!a.c.equals("x")&&!a.c.equals("z")){  
  217.           arrayList.add(a);  
  218.          }  
  219.         }  
  220.       for(Object a:arrayList){  
  221.       System.out.println(a);  
  222.       }  
  223.      }  
  224.      public pingpang(String a, String b, String c) {  
  225.       super();  
  226.       this.a = a;  
  227.       this.b = b;  
  228.       this.c = c;  
  229.      }  
  230.      @Override  
  231.      public String toString() {  
  232.       // TODO Auto-generated method stub  
  233.       return "a的对手是"+a+","+"b的对手是"+b+","+"c的对手是"+c+"\n";  
  234.      }  
  235. }  
  236. 【程序19】  题目:打印出如下图案(菱形)     
  237. *     
  238. ***     
  239. ******     
  240. ********     
  241. ******     
  242. ***     
  243. *     
  244. 1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重   for循环,第一层控制行,第二层控制列。     
  245. 三角形:  
  246. public class StartG {  
  247.    public static void main(String [] args)  
  248.    {  
  249.        int i=0;  
  250.        int j=0;  
  251.        for(i=1;i<=4;i++)  
  252.        {   for(j=1;j<=2*i-1;j++)  
  253.                System.out.print("*");  
  254.             System.out.println("");      
  255.        }  
  256.        for(i=4;i>=1;i--)  
  257.        { for(j=1;j<=2*i-3;j++)  
  258.                System.out.print("*");  
  259.                 System.out.println("");      
  260.        }  
  261.    }  
  262.  }  
  263.   
  264. 菱形:  
  265. public class StartG {  
  266.    public static void main(String [] args)  
  267.    {  
  268.        int i=0;  
  269.        int j=0;  
  270.        for(i=1;i<=4;i++)  
  271.        {  
  272.            for(int k=1; k<=4-i;k++)  
  273.              System.out.print(" ");  
  274.            for(j=1;j<=2*i-1;j++)  
  275.                System.out.print("*");  
  276.            System.out.println("");      
  277.        }  
  278.        for(i=4;i>=1;i--)  
  279.        {   
  280.            for(int k=1; k<=5-i;k++)  
  281.                  System.out.print(" ");  
  282.            for(j=1;j<=2*i-3;j++)  
  283.                System.out.print("*");  
  284.             System.out.println("");      
  285.        }  
  286.    }  
  287.  }  
  288.   
  289. 【程序20】   题目:有一分数序列:2/13/25/38/513/821/13...求出这个数列的前20项之和。     
  290. 1.程序分析:请抓住分子与分母的变化规律。     
  291. public class test20 {  
  292.  public static void main(String[] args) {  
  293.   float fm = 1f;  
  294.   float fz = 1f;  
  295.   float temp;  
  296.   float sum = 0f;  
  297.   for (int i=0;i<20;i++){  
  298.    temp = fm;  
  299.    fm = fz;  
  300.    fz = fz + temp;  
  301.    sum += fz/fm;  
  302.    //System.out.println(sum);  
  303.   }  
  304.   System.out.println(sum);  
  305.  }  
  306. }  
Java代码   收藏代码
  1. 【程序21】   题目:求1+2!+3!+...+20!的和      
  2. 1.程序分析:此程序只是把累加变成了累乘。      
  3. public class Ex21 {  
  4. static long sum = 0;    
  5. static long fac = 0;  
  6. public static void main(String[] args) {  
  7. long sum = 0;    
  8. long fac = 1;  
  9. for(int i=1; i<=10; i++) {  
  10. fac = fac * i;  
  11. sum += fac;  
  12. }  
  13. System.out.println(sum);  
  14. }  
  15. }  
  16. 【程序22】   题目:利用递归方法求5!。      
  17. 1.程序分析:递归公式:fn=fn_1*4!      
  18. import java.util.Scanner;  
  19. public class Ex22 {  
  20. public static void main(String[] args) {  
  21.   Scanner s = new Scanner(System.in);  
  22.   int n = s.nextInt();  
  23.   Ex22 tfr = new Ex22();  
  24.   System.out.println(tfr.recursion(n));  
  25.   
  26. }  
  27.   
  28. public long recursion(int n) {  
  29.   long value = 0 ;  
  30.   if(n ==1 || n == 0) {  
  31.   value = 1;  
  32.   } else if(n > 1) {  
  33.   value = n * recursion(n-1);  
  34.   }  
  35.   return value;  
  36. }  
  37.   
  38. }  
  39.   
  40. 【程序23】   题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?      
  41. 1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。      
  42. public class Ex23 {  
  43.   
  44. static int getAge(int n){  
  45. if (n==1){  
  46. return 10;  
  47. }  
  48. return 2 + getAge(n-1);  
  49. }  
  50. public static void main(String[] args) {  
  51. System.out.println("第五个的年龄为:"+getAge(5));  
  52. }  
  53. }  
  54. 【程序24】   题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。      
  55. import java.util.Scanner;  
  56. public class Ex24 {  
  57. public static void main(String[] args) {  
  58.   Ex24 tn = new Ex24();  
  59.   Scanner s = new Scanner(System.in);  
  60.   long a = s.nextLong();  
  61.   if(a < 0 || a > 100000) {  
  62.   System.out.println("Error Input, please run this program Again");  
  63.   System.exit(0);  
  64.   }  
  65.   if(a >=0 && a <=9) {  
  66.   System.out.println( a + "是一位数");  
  67.   System.out.println("按逆序输出是" + '\n' + a);  
  68.   } else if(a >= 10 && a <= 99) {  
  69.   System.out.println(a + "是二位数");  
  70.   System.out.println("按逆序输出是" );  
  71.   tn.converse(a);  
  72.   } else if(a >= 100 && a <= 999) {  
  73.   System.out.println(a + "是三位数");  
  74.   System.out.println("按逆序输出是" );  
  75.   tn.converse(a);  
  76.   } else if(a >= 1000 && a <= 9999) {  
  77.   System.out.println(a + "是四位数");  
  78.   System.out.println("按逆序输出是" );  
  79.   tn.converse(a);  
  80.   } else if(a >= 10000 && a <= 99999) {  
  81.   System.out.println(a + "是五位数");  
  82.   System.out.println("按逆序输出是" );  
  83.   tn.converse(a);  
  84.   }  
  85. }  
  86. public void converse(long l) {  
  87.   String s = Long.toString(l);  
  88.   char[] ch = s.toCharArray();  
  89.   for(int i=ch.length-1; i>=0; i--) {  
  90.   System.out.print(ch[i]);  
  91.   }  
  92. }  
  93. }  
  94. 【程序25】   题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。      
  95. import java.util.Scanner;  
  96. public class Ex25 {  
  97. static int[] a = new int[5];  
  98. static int[] b = new int[5];  
  99. public static void main(String[] args) {  
  100.   boolean is =false;  
  101.   Scanner s = new Scanner(System.in);  
  102.   long l = s.nextLong();  
  103.   if (l > 99999 || l < 10000) {  
  104.   System.out.println("Input error, please input again!");  
  105.   l = s.nextLong();  
  106.   }  
  107.   for (int i = 4; i >= 0; i--) {  
  108.   a[i] = (int) (l / (long) Math.pow(10, i));  
  109.   l =(l % ( long) Math.pow(10, i));  
  110.   }  
  111.   System.out.println();  
  112.   for(int i=0,j=0; i<5; i++, j++) {  
  113.   b[j] = a[i];  
  114.   }  
  115.   for(int i=0,j=4; i<5; i++, j--) {  
  116.   if(a[i] != b[j]) {  
  117.   is = false;  
  118.   break;  
  119.   } else {  
  120.   is = true;  
  121.   }  
  122.   }  
  123.   if(is == false) {  
  124.   System.out.println("is not a Palindrom!");  
  125.   } else if(is == true) {  
  126.   System.out.println("is a Palindrom!");  
  127.   }  
  128. }  
  129. }  
  130. 【程序26】   题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续   判断第二个字母。      
  131. 1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。      
  132. import java.util.Scanner;  
  133. public class Ex26 {  
  134.  public static void main(String[] args){  
  135.   //保存用户输入的第二个字母  
  136.   char weekSecond;  
  137.   //将Scanner类示例化为input对象,用于接收用户输入  
  138.   Scanner input = new Scanner(System.in);  
  139.   //开始提示并接收用户控制台输入    
  140.   System.out.print("请输入星期值英文的第一个字母,我来帮您判断是星期几:");  
  141.   String letter = input.next();  
  142.   //判断用户控制台输入字符串长度是否是一个字母  
  143.   if (letter.length() == 1){  
  144.   //利用取第一个索引位的字符来实现让Scanner接收char类型输入  
  145.   char weekFirst = letter.charAt(0);  
  146.   switch (weekFirst){  
  147.   case 'm':  
  148.   //当输入小写字母时,利用switch结构特性执行下一个带break语句的case分支,以实现忽略用户控制台输入大小写敏感的功能  
  149.   case 'M':  
  150.   System.out.println("星期一(Monday)");  
  151.   break;  
  152.   case 't':  
  153.   //当输入小写字母时,利用switch结构特性执行下一个带break语句的case分支,以实现忽略用户控制台输入大小写敏感的功能  
  154.   case 'T':  
  155.   System.out.print("由于星期二(Tuesday)与星期四(Thursday)均以字母T开头,故需输入第二个字母才能正确判断:");  
  156.   letter = input.next();  
  157.   //判断用户控制台输入字符串长度是否是一个字母  
  158.   if (letter.length() == 1){  
  159.   //利用取第一个索引位的字符来实现让Scanner接收char类型输入  
  160.   weekSecond = letter.charAt(0);  
  161.   //利用或(||)运算符来实现忽略用户控制台输入大小写敏感的功能  
  162.   if (weekSecond == 'U' || weekSecond == 'u'){  
  163.   System.out.println("星期二(Tuesday)");  
  164.   break;  
  165.   //利用或(||)运算符来实现忽略用户控制台输入大小写敏感的功能  
  166.   } else if (weekSecond == 'H' || weekSecond == 'h'){  
  167.   System.out.println("星期四(Thursday)");  
  168.   break;  
  169.   //控制台错误提示  
  170.   } else{  
  171.   System.out.println("输入错误,不能识别的星期值第二个字母,程序结束!");  
  172.   break;  
  173.   }  
  174.   } else {  
  175.   //控制台错误提示    
  176.   System.out.println("输入错误,只能输入一个字母,程序结束!");  
  177.   break;  
  178.   }  
  179.   case 'w':  
  180.   //当输入小写字母时,利用switch结构特性执行下一个带break语句的case分支,以实现忽略用户控制台输入大小写敏感的功能  
  181.   case 'W':  
  182.   System.out.println("星期三(Wednesday)");  
  183.   break;  
  184.   case 'f':  
  185.   //当输入小写字母时,利用switch结构特性执行下一个带break语句的case分支,以实现忽略用户控制台输入大小写敏感的功能  
  186.   case 'F':  
  187.   System.out.println("星期五(Friday)");  
  188.   break;  
  189.   case 's':  
  190.   //当输入小写字母时,利用switch结构特性执行下一个带break语句的case分支,以实现忽略用户控制台输入大小写敏感的功能  
  191.   case 'S':  
  192.   System.out.print("由于星期六(Saturday)与星期日(Sunday)均以字母S开头,故需输入第二个字母才能正确判断:");  
  193.   letter = input.next();  
  194.   //判断用户控制台输入字符串长度是否是一个字母  
  195.   if (letter.length() == 1){  
  196.   //利用取第一个索引位的字符来实现让Scanner接收char类型输入  
  197.   weekSecond = letter.charAt(0);  
  198.   //利用或(||)运算符来实现忽略用户控制台输入大小写敏感的功能  
  199.   if (weekSecond == 'A' || weekSecond == 'a'){  
  200.   System.out.println("星期六(Saturday)");  
  201.   break;  
  202.   //利用或(||)运算符来实现忽略用户控制台输入大小写敏感的功能  
  203.   } else if (weekSecond == 'U' || weekSecond == 'u'){  
  204.   System.out.println("星期日(Sunday)");  
  205.   break;  
  206.   //控制台错误提示  
  207.   } else{  
  208.   System.out.println("输入错误,不能识别的星期值第二个字母,程序结束!");  
  209.   break;  
  210.   }  
  211.   } else{  
  212.   //控制台错误提示    
  213.   System.out.println("输入错误,只能输入一个字母,程序结束!");  
  214.   break;  
  215.   }  
  216.   default:  
  217.   //控制台错误提示    
  218.   System.out.println("输入错误,不能识别的星期值第一个字母,程序结束!");  
  219.   break;  
  220.   }    
  221.   } else{  
  222.   //控制台错误提示    
  223.   System.out.println("输入错误,只能输入一个字母,程序结束!");  
  224.   }  
  225.  }  
  226. }  
  227.   
  228. 【程序27】   题目:求100之内的素数      
  229. public class Ex27 {  
  230.  public static void main(String args[])  
  231.  {  
  232.   int sum,i;  
  233.   for(sum=2;sum<=100;sum++)  
  234.   {  
  235.   for(i=2;i<=sum/2;i++)  
  236.   {  
  237.   if(sum%i==0)  
  238.   break;  
  239.   }  
  240.   if(i>sum/2)  
  241.   System.out.println(sum+"是素数");  
  242.   }  
  243.  }  
  244. }  
  245. 【程序28】   题目:对10个数进行排序      
  246. 1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,   下次类推,即用第二个元素与后8个进行比较,并进行交换。      
  247. import java.util.Arrays;  
  248. import java.util.Random;  
  249. import java.util.Scanner;  
  250. public class Ex28 {  
  251.  public static void main(String[] args) {  
  252.   int arr[] = new int[11];  
  253.   Random r=new Random();  
  254.   for(int i=0;i<10;i++){  
  255.   arr[i]=r.nextInt(100)+1;//得到10个100以内的整数  
  256.   }  
  257.   Arrays.sort(arr);  
  258.   for(int i=0;i<arr.length;i++){  
  259.   System.out.print(arr[i]+"\t");  
  260.   }  
  261.   System.out.print("\nPlease Input a int number: ");  
  262.   Scanner sc=new Scanner(System.in);  
  263.   arr[10]=sc.nextInt();//输入一个int值  
  264.   Arrays.sort(arr);  
  265.   for(int i=0;i<arr.length;i++){  
  266.   System.out.print(arr[i]+"\t");  
  267.   }  
  268.  }  
  269. }  
  270. 【程序29】   题目:求一个3*3矩阵对角线元素之和      
  271. 1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。      
  272. public class Ex29 {  
  273. public static void main(String[] args){  
  274. double sum=0;  
  275. int array[][]={{1,2,3},{4,56},{7,7,8}};  
  276. for(int i=0;i<3;i++)  
  277. for(int j=0;j<3;j++){  
  278. if(i==j)  
  279. sum=sum + array[i][j];  
  280. }  
  281. System.out.println( sum);     
  282. }  
  283. }  
  284. 【程序30】   题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。      
  285. 1.   程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。      
  286. import java.util.Random;  
  287. public class ArraySort {  
  288.   public static void main(String[] args)  
  289.   { int temp=0;  
  290. int myarr[] = new int[12];  
  291. Random r=new Random();  
  292. for(int i=1;i<=10;i++)  
  293. myarr[i]=r.nextInt(1000);  
  294. for (int k=1;k<=10;k++)  
  295. System.out.print(myarr[k]+",");  
  296. for(int i=1;i<=9;i++)  
  297. for(int k=i+1;k<=10;k++)  
  298. if(myarr[i]>myarr[k])  
  299. {  
  300. temp=myarr[i];  
  301. myarr[i]=myarr[k];  
  302. myarr[k]=temp;  
  303. }  
  304.   System.out.println("");  
  305. for (int k=1;k<=10;k++)  
  306. System.out.print(myarr[k]+",");  
  307.      
  308. myarr[11]=r.nextInt(1000);  
  309. for(int k=1;k<=10;k++)  
  310. if(myarr[k]>myarr[11])  
  311. {  
  312. temp=myarr[11];  
  313. for(int j=11;j>=k+1;j--)  
  314. myarr[j]=myarr[j-1];  
  315. myarr[k]=temp;  
  316. }  
  317. System.out.println("");     
  318. for (int k=1;k<=11;k++)  
  319. System.out.print(myarr[k]+",");  
  320.   }  
  321. }  
Java代码   收藏代码
  1. 【程序31】   题目:将一个数组逆序输出。     
  2. 程序分析:用第一个与最后一个交换。     
  3. 其实,用循环控制变量更简单:  
  4.        for(int k=11;k>=1;k--)  
  5.               System.out.print(myarr[k]+",");  
  6.   
  7. 【程序32】   题目:取一个整数a从右端开始的47位。     
  8. 程序分析:可以这样考虑:     
  9. (1)先使a右移4位。     
  10. (2)设置一个低4位全为1,其余全为0的数。可用~(~0 < <4)     
  11. (3)将上面二者进行&运算。     
  12.   
  13. public class Ex32 {  
  14.   public static void main(String[] args)  
  15.   {  
  16.     int a=0;  
  17.     long b=18745678;  
  18.     a=(int) Math.floor(b % Math.pow(10,7)/Math.pow(103));  
  19.     System.out.println(a);  
  20.   }  
  21.   }  
  22. 【程序33】     
  23. 题目:打印出杨辉三角形(要求打印出10行如下图)     
  24. 1.程序分析:     
  25. 1     
  26. 1   1     
  27. 1   2   1     
  28. 1   3   3   1     
  29. 1   4   6   4   1     
  30. 1   5   10   10   5   1     
  31. public class Ex33 {  
  32.     public static void main(String args[]){  
  33.            int i,j;  
  34.            int a[][];  
  35.            a=new int[8][8];  
  36.           for(i=0;i<8;i++){  
  37.              a[i][i]=1;  
  38.              a[i][0]=1;   
  39.             }  
  40.           for(i=2;i<8;i++){  
  41.            for(j=1;j<=i-1;j++){  
  42.           a[i][j]=a[i-1][j-1]+a[i-1][j];   
  43.            }  
  44.           }    
  45.           for(i=0;i<8;i++){  
  46.           for(j=0;j<i;j++){    
  47.            System.out.printf("  "+a[i][j]);  
  48.            }  
  49.           System.out.println();  
  50.           }  
  51.          }  
  52. }  
  53.   
  54. 【程序34】   题目:输入3个数a,b,c,按大小顺序输出。     
  55. 1.程序分析:利用指针方法。     
  56. public class Ex34 {  
  57.     public static void main(String[] args)   
  58.     {   
  59.     int []arrays = {800,56,500};   
  60.     for(int i=arrays.length;--i>=0;)   
  61.     {   
  62.     for(int j=0;j<i;j++)   
  63.     {   
  64.     if(arrays[j]>arrays[j+1])   
  65.     {   
  66.     int temp=arrays[j];   
  67.     arrays[j]=arrays[j+1];   
  68.     arrays[j+1]=temp;   
  69.     }   
  70.     }   
  71.     }   
  72.     for(int n=0;n<arrays.length;n++)   
  73.     System.out.println(arrays[n]);   
  74.     }   
  75.   
  76. }  
  77. 【程序35】   题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。     
  78. import java.util.*;   
  79. public class Ex35 {  
  80. public static void main(String[] args) {   
  81. int i, min, max, n, temp1, temp2;   
  82. int a[];   
  83. System.out.println("输入数组的长度:");   
  84. Scanner keyboard = new Scanner(System.in);   
  85. n = keyboard.nextInt();   
  86. a = new int[n];   
  87. for (i = 0; i < n; i++) {   
  88. System.out.print("输入第" + (i + 1) + "个数据");   
  89. a[i] = keyboard.nextInt();   
  90. }   
  91. //以上是输入整个数组  
  92. max = 0;   
  93. min = 0;   
  94. //设置两个标志,开始都指向第一个数  
  95. for (i = 1; i < n; i++) {   
  96. if (a[i] > a[max])   
  97. max = i; //遍历数组,如果大于a[max],就把他的数组下标赋给max  
  98. if (a[i] < a[min])   
  99. min = i; //同上,如果小于a[min],就把他的数组下标赋给min  
  100. }   
  101. //以上for循环找到最大值和最小值,max是最大值的下标,min是最小值的下标  
  102. temp1 = a[0];   
  103. temp2 = a[min]; //这两个temp只是为了在交换时使用  
  104.   
  105. a[0] = a[max];   
  106. a[max] = temp1; //首先交换a[0]和最大值a[max]  
  107.   
  108. if (min != 0) { //如果最小值不是a[0],执行下面  
  109. a[min] = a[n - 1];   
  110. a[n - 1] = temp2; //交换a[min]和a[n-1]  
  111. else {       //如果最小值是a[0],执行下面  
  112. a[max] = a[n - 1];   
  113. a[n - 1] = temp1;   
  114. }   
  115.   
  116. for (i = 0; i < n; i++) { //输出数组  
  117. System.out.print(a[i] + " ");   
  118. }   
  119. }  
  120. }  
  121. 【程序36】   题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数     
  122.   
  123.   
  124. 【程序37】     
  125. 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从13报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。     
  126. import java.util.Scanner;  
  127. public class Ex37 {  
  128.     public static void main(String[] args) {  
  129.            Scanner s = new Scanner(System.in);  
  130.            int n = s.nextInt();  
  131.            boolean[] arr = new boolean[n];  
  132.            for(int i=0; i<arr.length; i++) {  
  133.             arr[i] = true;//下标为TRUE时说明还在圈里  
  134.            }  
  135.            int leftCount = n;  
  136.            int countNum = 0;  
  137.            int index = 0;  
  138.            while(leftCount > 1) {  
  139.             if(arr[index] == true) {//当在圈里时  
  140.              countNum ++; //报数递加  
  141.              if(countNum == 3) {//报道3时  
  142.               countNum =0;//从零开始继续报数  
  143.               arr[index] = false;//此人退出圈子  
  144.               leftCount --;//剩余人数减一  
  145.              }  
  146.             }  
  147.             index ++;//每报一次数,下标加一  
  148.             if(index == n) {//是循环数数,当下标大于n时,说明已经数了一圈,  
  149.              index = 0;//将下标设为零重新开始。  
  150.             }  
  151.            }  
  152.            for(int i=0; i<n; i++) {  
  153.             if(arr[i] == true) {  
  154.              System.out.println(i);  
  155.             }  
  156.            }  
  157.      }  
  158. }  
  159.   
  160. 【程序38】     
  161. 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。     
  162. import java.util.Scanner;  
  163. public class Ex38 {  
  164. public static void main(String [] args)  
  165. {  
  166.     Scanner s = new Scanner(System.in);  
  167.     System.out.println("请输入一个字符串");  
  168.     String mys= s.next();  
  169.     System.out.println(str_len(mys));  
  170. }  
  171.   public static int str_len(String x)  
  172.   {  
  173.       return x.length();  
  174.   }  
  175. }  
  176.   
  177.   
  178.     
  179. 题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n  
  180.   
  181. 【程序39】    
  182. 题目:字符串排序。     
  183.   
  184. import java.util.*;     
  185. public class test{  
  186.     public   static   void   main(String[]   args)  
  187.     {     
  188.      ArrayList<String> list=new ArrayList<String>();     
  189.      list.add("010101");     
  190.      list.add("010003");     
  191.     list.add("010201");     
  192.     Collections.sort(list);     
  193.   for(int   i=0;i<list.size();i++){     
  194.   System.out.println(list.get(i));     
  195.   }     
  196.   }     
  197.   }  
  198.   
  199. 【程序40】     
  200. 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?     
  201.   
  202. public class Dg {  
  203. static int ts=0;//桃子总数  
  204. int fs=1;//记录分的次数  
  205. static int hs=5;//猴子数...  
  206. int tsscope=5000;//桃子数的取值范围.太大容易溢出.  
  207. public int fT(int t){  
  208. if(t==tsscope){  
  209. //当桃子数到了最大的取值范围时取消递归  
  210. System.out.println("结束");  
  211. return 0;  
  212. }  
  213. else{  
  214. if((t-1)%hs==0 && fs <=hs){  
  215. if(fs==hs)  
  216. {  
  217. System.out.println("桃子数 = "+ts +" 时满足分桃条件");  
  218. }  
  219.    fs+=1;  
  220.    return fT((t-1)/5*4);// 返回猴子拿走一份后的剩下的总数  
  221. }  
  222. else  
  223. {  
  224. //没满足条件  
  225. fs=1;//分的次数重置为1  
  226. return fT(ts+=1);//桃子数加+1  
  227. }  
  228. }  
  229. }  
  230. public static void main(String[] args) {  
  231. new Dg().fT(0);  
  232. }  
  233.   


  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值