Java经典算法四十例编程详解+程序实例

这篇博客详细讲解了10个经典的Java算法实现,包括斐波那契数列、素数判断、水仙花数、分解质因数、求最大公约数和最小公倍数、统计字符类型、循环求和、判断完数、打印杨辉三角形、数组排序等。通过这些实例,读者可以深入理解并掌握Java编程中的基础算法知识。
摘要由CSDN通过智能技术生成
[java]  view plain  copy
  1. JAVA经典算法40例  
  2. 【程序1】   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?     
  3. 1.程序分析:   兔子的规律为数列1,1,2,3,5,8,13,21....     
  4. public class exp2{  
  5.     public static void main(String args[]){  
  6.         int i=0;  
  7.         for(i=1;i<=20;i++)  
  8.             System.out.println(f(i));  
  9.     }  
  10.     public static int f(int x)  
  11.     {  
  12.         if(x==1 || x==2)  
  13.             return 1;  
  14.         else  
  15.             return f(x-1)+f(x-2);  
  16.     }  
  17. }  
  18. 或  
  19. public class exp2{  
  20.     public static void main(String args[]){  
  21.         int i=0;  
  22.         math mymath = new math();  
  23.         for(i=1;i<=20;i++)  
  24.             System.out.println(mymath.f(i));  
  25.     }  
  26.   
  27. }  
  28. class math  
  29. {  
  30.     public int f(int x)  
  31.     {  
  32.         if(x==1 || x==2)  
  33.             return 1;  
  34.         else  
  35.             return f(x-1)+f(x-2);  
  36.     }  
  37. }  
  38.   
  39. 【程序2】   题目:判断101-200之间有多少个素数,并输出所有素数。     
  40. 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,     
  41. 则表明此数不是素数,反之是素数。     
  42. public class exp2{  
  43.     public static void main(String args[]){  
  44.         int i=0;  
  45.         math mymath = new math();  
  46.         for(i=2;i<=200;i++)  
  47.             if(mymath.iszhishu(i)==true)  
  48.             System.out.println(i);  
  49.     }  
  50. }  
  51. class math  
  52. {  
  53.     public int f(int x)  
  54.     {  
  55.         if(x==1 || x==2)  
  56.             return 1;  
  57.         else  
  58.             return f(x-1)+f(x-2);  
  59.     }  
  60.     public boolean iszhishu(int x)  
  61.     {  
  62.         for(int i=2;i<=x/2;i++)  
  63.             if (x % 2==0 )  
  64.                 return false;  
  65.         return true;  
  66.     }  
  67. }  
  68.   
  69. 【程序3】   题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。     
  70. 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。     
  71. public class exp2{  
  72.     public static void main(String args[]){  
  73.         int i=0;  
  74.         math mymath = new math();  
  75.         for(i=100;i<=999;i++)  
  76.             if(mymath.shuixianhua(i)==true)  
  77.             System.out.println(i);  
  78.     }  
  79. }  
  80. class math  
  81. {  
  82.     public int f(int x)  
  83.     {  
  84.         if(x==1 || x==2)  
  85.             return 1;  
  86.         else  
  87.             return f(x-1)+f(x-2);  
  88.     }  
  89.     public boolean iszhishu(int x)  
  90.     {  
  91.         for(int i=2;i<=x/2;i++)  
  92.             if (x % i==0 )  
  93.                 return false;  
  94.         return true;  
  95.     }  
  96.     public boolean shuixianhua(int x)  
  97.     {  
  98.        int i=0,j=0,k=0;  
  99.        i=x / 100;  
  100.        j=(x % 100) /10;  
  101.        k=x % 10;  
  102.        if(x==i*i*i+j*j*j+k*k*k)  
  103.            return true;  
  104.        else  
  105.            return false;  
  106.          
  107.     }  
  108. }  
  109. 【程序4】   题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。     
  110. 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:     
  111. (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。     
  112. (2)如果n <> k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你,重复执行第一步。     
  113. (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。     
  114. public class exp2{  
  115.     public exp2(){}  
  116.     public void fengjie(int n){  
  117.         for(int i=2;i<=n/2;i++){  
  118.             if(n%i==0){  
  119.                 System.out.print(i+"*");  
  120.                 fengjie(n/i);  
  121.                 }  
  122.         }  
  123.         System.out.print(n);  
  124.         System.exit(0);///不能少这句,否则结果会出错  
  125.         }  
  126.         public static void main(String[] args){  
  127.              String str="";  
  128.              exp2 c=new exp2();  
  129.              str=javax.swing.JOptionPane.showInputDialog("请输入N的值(输入exit退出):");  
  130.              int N;  
  131.              N=0;  
  132.              try{  
  133.                      N=Integer.parseInt(str);  
  134.                      }catch(NumberFormatException e){  
  135.                          e.printStackTrace();  
  136.                          }  
  137.             System.out.print(N+"分解质因数:"+N+"=");  
  138.             c.fengjie(N);  
  139.         }      
  140. }  
  141. 【程序5】   题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。     
  142. 1.程序分析:(a> b)?a:b这是条件运算符的基本例子。     
  143. import javax.swing.*;  
  144. public class ex5 {  
  145.         public static void main(String[] args){  
  146.              String str="";  
  147.              str=JOptionPane.showInputDialog("请输入N的值(输入exit退出):");  
  148.              int N;  
  149.              N=0;  
  150.              try{  
  151.                 N=Integer.parseInt(str);  
  152.               }  
  153.              catch(NumberFormatException e){  
  154.                 e.printStackTrace();  
  155.                }  
  156.              str=(N>90?"A":(N>60?"B":"C"));  
  157.              System.out.println(str);  
  158.         }      
  159. }  
  160. 【程序6】   题目:输入两个正整数m和n,求其最大公约数和最小公倍数。     
  161. 1.程序分析:利用辗除法。     
  162. 最大公约数:  
  163. public class CommonDivisor{  
  164.     public static void main(String args[])  
  165.     {  
  166.         commonDivisor(24,32);  
  167.     }  
  168.     static int commonDivisor(int M, int N)  
  169.     {  
  170.         if(N<0||M<0)  
  171.         {  
  172.             System.out.println("ERROR!");  
  173.             return -1;  
  174.         }  
  175.         if(N==0)  
  176.         {  
  177.             System.out.println("the biggest common divisor is :"+M);  
  178.             return M;  
  179.         }  
  180.         return commonDivisor(N,M%N);  
  181.     }  
  182. }  
  183. 最小公倍数和最大公约数:  
  184. import java.util.Scanner;   
  185. public class CandC   
  186. {   
  187. //下面的方法是求出最大公约数  
  188. public static int gcd(int m, int n)   
  189. {   
  190. while (true)   
  191. {   
  192. if ((m = m % n) == 0)   
  193. return n;   
  194. if ((n = n % m) == 0)   
  195. return m;   
  196. }   
  197. }   
  198. public static void main(String args[]) throws Exception   
  199. {   
  200. //取得输入值  
  201. //Scanner chin = new Scanner(System.in);   
  202. //int a = chin.nextInt(), b = chin.nextInt();   
  203. int a=23int b=32;  
  204. int c = gcd(a, b);   
  205. System.out.println("最小公倍数:" + a * b / c + "\n最大公约数:" + c);   
  206. }   
  207. }  
  208. 【程序7】   题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。     
  209. 1.程序分析:利用while语句,条件为输入的字符不为 '\n '.     
  210. import java.util.Scanner;  
  211. public class ex7 {  
  212.      public static void main(String args[])  
  213.      {  
  214.       System.out.println("请输入字符串:");  
  215.       Scanner scan=new Scanner(System.in);  
  216.       String str=scan.next();  
  217.       String E1="[\u4e00-\u9fa5]";  
  218.       String E2="[a-zA-Z]";  
  219.       int countH=0;  
  220.       int countE=0;  
  221.       char[] arrChar=str.toCharArray();  
  222.       String[] arrStr=new String[arrChar.length];  
  223.       for (int i=0;i<arrChar.length ;i++ )  
  224.       {  
  225.        arrStr[i]=String.valueOf(arrChar[i]);  
  226.       }  
  227.       for (String i: arrStr )  
  228.       {  
  229.        if (i.matches(E1))  
  230.        {  
  231.         countH++;  
  232.        }  
  233.        if (i.matches(E2))  
  234.        {  
  235.         countE++;  
  236.        }  
  237.       }  
  238.       System.out.println("汉字的个数"+countH);  
  239.       System.out.println("字母的个数"+countE);  
  240.      }  
  241.     }   
  242. 【程序8】   题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。     
  243. 1.程序分析:关键是计算出每一项的值。     
  244. import java.io.*;  
  245. public class Sumloop {  
  246.   public static void main(String[] args) throws IOException  
  247.   {  
  248.       int s=0;  
  249.       String output="";  
  250.       BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in));  
  251.       System.out.println("请输入a的值");  
  252.       String input =stadin.readLine();  
  253.       for(int i =1;i<=Integer.parseInt(input);i++)  
  254.       {  
  255.           output+=input;  
  256.           int a=Integer.parseInt(output);  
  257.           s+=a;  
  258.       }  
  259.    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值