java 一些算法题目

来源:互联网 发布:甲醛有味道吗 知乎 编辑:程序博客网 时间:2024/05/18 19:22
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. }  
原创粉丝点击