java 一些算法題目

Monkey_peach代碼 複製代碼
  1. package com.sailor.game;   
  2.   
  3. /**   
  4.  * 題目:猴子喫桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩   
  5.  * 下的桃子喫掉一半,又多吃了一個。以後每天早上都吃了前一天剩下的一半零一個。到第10天早上想再喫時,見只剩下一個桃子了。求第一天共摘了多少。   
  6.  * 程序分析:採取逆向思維的方法,從後往前推斷。   
  7.  *    
  8.  * @author Sailor   
  9.  *    
  10.  */   
  11. public class Monkey_Peach {   
  12.   
  13.     public static void main(String[] args) {   
  14.         int[] peach = new int[10];   
  15.         peach[9] = 1;   
  16.         // 下面利用的是數組和循環將每天的桃子數量都求出來了   
  17.         for (int i = peach.length - 1; i > 0; i--) {   
  18.             peach[i - 1] = 2 * (peach[i] + 1);   
  19.         }   
  20.         for (int i = 0; i < peach.length; i++) {   
  21.             System.out.println(peach[i]);   
  22.         }   
  23.         System.out.println("第一天的桃子數:"+getPeach_Num(101));   
  24.     }   
  25.   
  26.     // 利用遞歸的方法來求第一天的桃子數,輸入參數爲天數和當天的桃子數,輸出爲第一天桃子數   
  27.     public static int getPeach_Num(int day, int peach_num) {   
  28.         if (day == 1)   
  29.             return peach_num;   
  30.         else if (day < 1 || peach_num < 0)   
  31.             return 0;   
  32.         else   
  33.             return getPeach_Num(day - 1, (peach_num + 1) * 2);   
  34.     }   
  35.   
  36. }  

 

 

Times_table代碼 複製代碼
  1. package com.sailor.game;   
  2.   
  3. /**   
  4.  * 輸出9*9口訣   
  5.  *    
  6.  * @author Sailor   
  7.  *    
  8.  */   
  9. public class Times_Table {   
  10.   
  11.     public static void main(String[] args) {   
  12.         for (int i = 1; i <= 9; i++) {   
  13.             for (int j = 1; j <= i; j++) {   
  14.                 System.out.print(j + " * " + i + " = " + (i * j));   
  15.                 System.out.print("/t");   
  16.             }   
  17.             System.out.println();   
  18.         }   
  19.     }   
  20.   
  21. }  

  

 

   

Armstrong_number代碼 複製代碼
  1. package com.sailor.game;   
  2.   
  3. /**   
  4.  * 題目:打印出所有的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數本身。   
  5.  * 例如:153是一個"水仙花數",因爲153=1的三次方+5的三次方+3的三次方。   
  6.  *    
  7.  * @author Sailor   
  8.  *    
  9.  */   
  10. public class Armstrong_number {   
  11.   
  12.     public static void main(String[] args) {   
  13.         for (int i = 100; i < 1000; i++) {   
  14.             int n1, n2, n3;   
  15.             int k = i;   
  16.             n1 = k / 100;   
  17.             k %= 100;   
  18.             n2 = k / 10;   
  19.             k %= 10;   
  20.             n3 = k;   
  21.             if (i == (getCube(n1) + getCube(n2) + getCube(n3))) {   
  22.                 System.out.println(i);   
  23.             }   
  24.         }   
  25.     }   
  26.   
  27.     public static int getCube(int n) {   
  28.         return n * n * n;   
  29.     }   
  30.   
  31. }  

 

  

   

Common_divisor代碼 複製代碼
  1. package com.sailor.game;   
  2.   
  3. import java.util.Scanner;   
  4.   
  5. /**   
  6.  * 題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。 程序分析:利用輾除法。   
  7.  *    
  8.  * 用輾轉相除法求兩個數的最大公約數的步驟如下: 先用小的一個數除大的一個數,得第一個餘數; 再用第一個餘數除小的一個數,得第二個餘數;   
  9.  * 又用第二個餘數除第一個餘數,得第三個餘數;   
  10.  * 這樣逐次用後一個數去除前一個餘數,直到餘數是0爲止。那麼,最後一個除數就是所求的最大公約數(如果最後的除數是1,那麼原來的兩個數是互質數)。   
  11.  * 最小公倍數爲兩個數相乘然後除以最大公約數   
  12.  *    
  13.  * @author sailor   
  14.  *    
  15.  */   
  16. public class Common_Divisor {   
  17.   
  18.     public static void main(String[] args) {   
  19.         Scanner in=new Scanner(System.in);   
  20.         System.out.println("請輸入第一個數");   
  21.         int a=in.nextInt();   
  22.         System.out.println("請輸入第二個數");   
  23.         int b=in.nextInt();   
  24.         System.out.println(a+"和"+b+"的最大公約數是"+getMaxCommon_Divisor(a, b));   
  25.         System.out.println(a+"和"+b+"的最小公倍數是"+getMincommon_multiple(a, b));   
  26.     }   
  27.   
  28.     // 求最大公約數   
  29.     public static int getMaxCommon_Divisor(int a, int b) {   
  30.         int max = Math.max(a, b);   
  31.         int min = Math.min(a, b);   
  32.         int mod = max % min;   
  33.         if (mod == 0) {   
  34.             return min;   
  35.         } else {   
  36.             return getMaxCommon_Divisor(mod, min);   
  37.         }   
  38.     }   
  39.   
  40.     // 求最大公約數   
  41.     public static int getMincommon_multiple(int a, int b) {   
  42.         return (a * b) / getMaxCommon_Divisor(a, b);   
  43.     }   
  44.   
  45. }  

 

 

  

Compute_day代碼 複製代碼
  1. package com.sailor.game;   
  2.   
  3. import java.util.Scanner;   
  4.   
  5. /**   
  6.  * 題目:輸入某年某月某日,判斷這一天是這一年的第幾天?   
  7.  * 程序分析:以35日爲例,應該先把前兩個月的加起來,然後再加上5天即本年的第幾天,   
  8.  * 特殊情況,閏年且輸入月份大於3時需考慮多加一天。   
  9.  *    
  10.  * @author Sailor   
  11.  *    
  12.  */   
  13. public class Compute_Day {   
  14.   
  15.     public static void main(String[] args) {   
  16.   
  17.         Scanner in = new Scanner(System.in);   
  18.         System.out.println("請依次輸入年、月、日:");   
  19.         int year = in.nextInt();   
  20.         int month = in.nextInt();   
  21.         int day = in.nextInt();   
  22.         int days = 0;   
  23.         boolean isLeap = isLeap(year);   
  24.         for (int i = 1; i < month; i++) {   
  25.             days += getDays(i, isLeap);   
  26.         }   
  27.         System.out.println("這是今年的第 " + (days + day) + " 天 ");   
  28.     }   
  29.   
  30.     public static int getDays(int month, boolean isLeap) {   
  31.         int days = 0;   
  32.         switch (month) {   
  33.         case 1:   
  34.         case 3:   
  35.         case 5:   
  36.         case 7:   
  37.         case 8:   
  38.         case 10:   
  39.         case 12:   
  40.             days = 31;   
  41.             break;   
  42.         case 4:   
  43.         case 6:   
  44.         case 9:   
  45.         case 11:   
  46.             days = 30;   
  47.             break;   
  48.         case 2:   
  49.             if (isLeap)   
  50.                 days = 29;   
  51.             else   
  52.                 days = 28;   
  53.             break;   
  54.         }   
  55.   
  56.         return days;   
  57.     }   
  58.   
  59.     /**   
  60.      * 判斷閏年的條件: 如果年份值能被4整除且不能被100整除,或者能被400整除,就是閏年,否則不是   
  61.      *    
  62.      * @param year   
  63.      * @return   
  64.      */   
  65.     public static boolean isLeap(int year) {   
  66.         if (year % 4 == 0 && year % 100 != 0) {   
  67.             return true;   
  68.         }   
  69.         if (year % 400 == 0) {   
  70.             return true;   
  71.         }   
  72.         return false;   
  73.     }   
  74.   
  75. }  

 

  

Gamelist代碼 複製代碼
  1. package com.sailor.game;   
  2.   
  3. /**   
  4.  * 題目:兩個乒乓球隊進行比賽,各出三人。甲隊爲a,b,c三人,乙隊爲x,y,z三人。   
  5.  * 已抽籤決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。   
  6.  *    
  7.  * @author Sailor   
  8.  *    
  9.  */   
  10. public class GameList {   
  11.   
  12.     public static void main(String[] args) {   
  13.         int a, b, c;   
  14.         int x = 1,z = 3;// y = 2,  此處y不用,只是爲了方便讀懂。   
  15.         String[] temp = { "x""y""z" };   
  16.         for (int i = 1; i <= 3; i++)   
  17.             for (int j = 1; j <= 3; j++)   
  18.                 for (int k = 1; k <= 3; k++) {   
  19.                     if (i != j && j != k && i != k) {   
  20.                         a = i;   
  21.                         b = j;   
  22.                         c = k;   
  23.                         if (a != x && c != x && c != z) {   
  24.                             System.out.println("a--" + temp[a - 1]);   
  25.                             System.out.println("b--" + temp[b - 1]);   
  26.                             System.out.println("c--" + temp[c - 1]);   
  27.                         }   
  28.                     }   
  29.                 }   
  30.     }   
  31.   
  32. }  

 

  

Monkeygetpeach代碼 複製代碼
  1. package com.sailor.game;   
  2.   
  3. /**   
  4.  * 題目:海灘上有一堆桃子,五隻猴子來分。第一隻猴子把這堆桃子憑據分爲五份,多了一個,這隻猴子把多的一個扔入海中,拿走了一份。   
  5.  * 第二隻猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中,拿走了一份,第三、第四、第五隻猴子都是這樣做的,   
  6.  * 問海灘上原來最少有多少個桃子? 解:最少的情況下,第五隻猴子分配時每隻猴子得到一個桃子,這是第五隻猴子看到的桃子是6個   
  7.  *    
  8.  * @author Sailor   
  9.  *    
  10.  */   
  11. public class MonkeyGetPeach {   
  12.   
  13.     public static void main(String[] args) {   
  14.         System.out.println(getPeach_Num(1));   
  15.     }   
  16.   
  17.     // 返回桃子總數   
  18.     public static int getPeach_Num(int index) {   
  19.         if (index == 5)   
  20.             return 6;   
  21.         if (index < 5) {   
  22.             return getPeach_Num(index + 1) * 5 + 1;   
  23.         } else   
  24.             return 0;   
  25.     }   
  26.   
  27. }  

 

 

Prime_factor代碼 複製代碼
  1. package com.sailor.game;   
  2.   
  3. import java.util.Scanner;   
  4.   
  5. /**   
  6.  *    
  7.  * 題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。 程序分析:對n進行分解質因數,應先找到一個最小的質數k,然後按下述步驟完成:   
  8.  * (1)如果這個質數恰等於n,則說明分解質因數的過程已經結束,打印出即可。 (2)如果n<>k,但n能被k整除,則應打印出k的值,並用n除以k的商,作爲新的正整數你n,重複執行第一步。   
  9.  * (3)如果n不能被k整除,則用k+1作爲k的值,重複執行第一步。   
  10.  *    
  11.  * @author Sailor   
  12.  */   
  13. public class Prime_factor {   
  14.   
  15.     public static void main(String[] args){   
  16.            
  17.         Scanner in=new Scanner(System.in);   
  18.         System.out.println("請輸入待分解因式的數字:");   
  19.         int num = in.nextInt();   
  20.         String result = num+"=";   
  21.         while (num > 1) {   
  22.             for (int i = 1; i < num;) {   
  23.                 int prime = getPrime(i);   
  24.                 if (num % prime == 0) {//從最小的質數開始找起   
  25.                     num /= prime;   
  26.                     result += prime + (num<=1 ? "" : "*");   
  27.                 }else{   
  28.                     i=prime;   
  29.                 }   
  30.             }   
  31.         }   
  32.         System.out.println(result);   
  33.     }   
  34.   
  35.     /**   
  36.      * 返回比n大的最小質數   
  37.      *    
  38.      * @param n   
  39.      * @return   
  40.      */   
  41.     public static int getPrime(int n) {   
  42.         int prime = 1;   
  43.         for (int i = 1 + n;; i++) {   
  44.             boolean mark = true;   
  45.             for (int j = 2; j <= Math.sqrt(i) + 1; j++) {   
  46.                 if (i % j == 0 && j != i) {   
  47.                     mark = false;   
  48.                     break;   
  49.                 }   
  50.             }   
  51.             if (mark) {   
  52.                 prime = i;   
  53.                 break;   
  54.             }   
  55.         }   
  56.         return prime;   
  57.     }   
  58.   
  59. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章