- package com.sailor.game;
- /**
- * 題目:猴子喫桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩
- * 下的桃子喫掉一半,又多吃了一個。以後每天早上都吃了前一天剩下的一半零一個。到第10天早上想再喫時,見只剩下一個桃子了。求第一天共摘了多少。
- * 程序分析:採取逆向思維的方法,從後往前推斷。
- *
- * @author Sailor
- *
- */
- public class Monkey_Peach {
- public static void main(String[] args) {
- int[] peach = new int[10];
- peach[9] = 1;
- // 下面利用的是數組和循環將每天的桃子數量都求出來了
- for (int i = peach.length - 1; i > 0; i--) {
- peach[i - 1] = 2 * (peach[i] + 1);
- }
- for (int i = 0; i < peach.length; i++) {
- System.out.println(peach[i]);
- }
- System.out.println("第一天的桃子數:"+getPeach_Num(10, 1));
- }
- // 利用遞歸的方法來求第一天的桃子數,輸入參數爲天數和當天的桃子數,輸出爲第一天桃子數
- public static int getPeach_Num(int day, int peach_num) {
- if (day == 1)
- return peach_num;
- else if (day < 1 || peach_num < 0)
- return 0;
- else
- return getPeach_Num(day - 1, (peach_num + 1) * 2);
- }
- }
- package com.sailor.game;
- /**
- * 輸出9*9口訣
- *
- * @author Sailor
- *
- */
- public class Times_Table {
- public static void main(String[] args) {
- for (int i = 1; i <= 9; i++) {
- for (int j = 1; j <= i; j++) {
- System.out.print(j + " * " + i + " = " + (i * j));
- System.out.print("/t");
- }
- System.out.println();
- }
- }
- }
- package com.sailor.game;
- /**
- * 題目:打印出所有的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數本身。
- * 例如:153是一個"水仙花數",因爲153=1的三次方+5的三次方+3的三次方。
- *
- * @author Sailor
- *
- */
- public class Armstrong_number {
- public static void main(String[] args) {
- for (int i = 100; i < 1000; i++) {
- int n1, n2, n3;
- int k = i;
- n1 = k / 100;
- k %= 100;
- n2 = k / 10;
- k %= 10;
- n3 = k;
- if (i == (getCube(n1) + getCube(n2) + getCube(n3))) {
- System.out.println(i);
- }
- }
- }
- public static int getCube(int n) {
- return n * n * n;
- }
- }
- package com.sailor.game;
- import java.util.Scanner;
- /**
- * 題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。 程序分析:利用輾除法。
- *
- * 用輾轉相除法求兩個數的最大公約數的步驟如下: 先用小的一個數除大的一個數,得第一個餘數; 再用第一個餘數除小的一個數,得第二個餘數;
- * 又用第二個餘數除第一個餘數,得第三個餘數;
- * 這樣逐次用後一個數去除前一個餘數,直到餘數是0爲止。那麼,最後一個除數就是所求的最大公約數(如果最後的除數是1,那麼原來的兩個數是互質數)。
- * 最小公倍數爲兩個數相乘然後除以最大公約數
- *
- * @author sailor
- *
- */
- public class Common_Divisor {
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- System.out.println("請輸入第一個數");
- int a=in.nextInt();
- System.out.println("請輸入第二個數");
- int b=in.nextInt();
- System.out.println(a+"和"+b+"的最大公約數是"+getMaxCommon_Divisor(a, b));
- System.out.println(a+"和"+b+"的最小公倍數是"+getMincommon_multiple(a, b));
- }
- // 求最大公約數
- public static int getMaxCommon_Divisor(int a, int b) {
- int max = Math.max(a, b);
- int min = Math.min(a, b);
- int mod = max % min;
- if (mod == 0) {
- return min;
- } else {
- return getMaxCommon_Divisor(mod, min);
- }
- }
- // 求最大公約數
- public static int getMincommon_multiple(int a, int b) {
- return (a * b) / getMaxCommon_Divisor(a, b);
- }
- }
- package com.sailor.game;
- import java.util.Scanner;
- /**
- * 題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
- * 程序分析:以3月5日爲例,應該先把前兩個月的加起來,然後再加上5天即本年的第幾天,
- * 特殊情況,閏年且輸入月份大於3時需考慮多加一天。
- *
- * @author Sailor
- *
- */
- public class Compute_Day {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println("請依次輸入年、月、日:");
- int year = in.nextInt();
- int month = in.nextInt();
- int day = in.nextInt();
- int days = 0;
- boolean isLeap = isLeap(year);
- for (int i = 1; i < month; i++) {
- days += getDays(i, isLeap);
- }
- System.out.println("這是今年的第 " + (days + day) + " 天 ");
- }
- public static int getDays(int month, boolean isLeap) {
- int days = 0;
- switch (month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- days = 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- days = 30;
- break;
- case 2:
- if (isLeap)
- days = 29;
- else
- days = 28;
- break;
- }
- return days;
- }
- /**
- * 判斷閏年的條件: 如果年份值能被4整除且不能被100整除,或者能被400整除,就是閏年,否則不是
- *
- * @param year
- * @return
- */
- public static boolean isLeap(int year) {
- if (year % 4 == 0 && year % 100 != 0) {
- return true;
- }
- if (year % 400 == 0) {
- return true;
- }
- return false;
- }
- }
- package com.sailor.game;
- /**
- * 題目:兩個乒乓球隊進行比賽,各出三人。甲隊爲a,b,c三人,乙隊爲x,y,z三人。
- * 已抽籤決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。
- *
- * @author Sailor
- *
- */
- public class GameList {
- public static void main(String[] args) {
- int a, b, c;
- int x = 1,z = 3;// y = 2, 此處y不用,只是爲了方便讀懂。
- String[] temp = { "x", "y", "z" };
- for (int i = 1; i <= 3; i++)
- for (int j = 1; j <= 3; j++)
- for (int k = 1; k <= 3; k++) {
- if (i != j && j != k && i != k) {
- a = i;
- b = j;
- c = k;
- if (a != x && c != x && c != z) {
- System.out.println("a--" + temp[a - 1]);
- System.out.println("b--" + temp[b - 1]);
- System.out.println("c--" + temp[c - 1]);
- }
- }
- }
- }
- }
- package com.sailor.game;
- /**
- * 題目:海灘上有一堆桃子,五隻猴子來分。第一隻猴子把這堆桃子憑據分爲五份,多了一個,這隻猴子把多的一個扔入海中,拿走了一份。
- * 第二隻猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中,拿走了一份,第三、第四、第五隻猴子都是這樣做的,
- * 問海灘上原來最少有多少個桃子? 解:最少的情況下,第五隻猴子分配時每隻猴子得到一個桃子,這是第五隻猴子看到的桃子是6個
- *
- * @author Sailor
- *
- */
- public class MonkeyGetPeach {
- public static void main(String[] args) {
- System.out.println(getPeach_Num(1));
- }
- // 返回桃子總數
- public static int getPeach_Num(int index) {
- if (index == 5)
- return 6;
- if (index < 5) {
- return getPeach_Num(index + 1) * 5 + 1;
- } else
- return 0;
- }
- }
- package com.sailor.game;
- import java.util.Scanner;
- /**
- *
- * 題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。 程序分析:對n進行分解質因數,應先找到一個最小的質數k,然後按下述步驟完成:
- * (1)如果這個質數恰等於n,則說明分解質因數的過程已經結束,打印出即可。 (2)如果n<>k,但n能被k整除,則應打印出k的值,並用n除以k的商,作爲新的正整數你n,重複執行第一步。
- * (3)如果n不能被k整除,則用k+1作爲k的值,重複執行第一步。
- *
- * @author Sailor
- */
- public class Prime_factor {
- public static void main(String[] args){
- Scanner in=new Scanner(System.in);
- System.out.println("請輸入待分解因式的數字:");
- int num = in.nextInt();
- String result = num+"=";
- while (num > 1) {
- for (int i = 1; i < num;) {
- int prime = getPrime(i);
- if (num % prime == 0) {//從最小的質數開始找起
- num /= prime;
- result += prime + (num<=1 ? "" : "*");
- }else{
- i=prime;
- }
- }
- }
- System.out.println(result);
- }
- /**
- * 返回比n大的最小質數
- *
- * @param n
- * @return
- */
- public static int getPrime(int n) {
- int prime = 1;
- for (int i = 1 + n;; i++) {
- boolean mark = true;
- for (int j = 2; j <= Math.sqrt(i) + 1; j++) {
- if (i % j == 0 && j != i) {
- mark = false;
- break;
- }
- }
- if (mark) {
- prime = i;
- break;
- }
- }
- return prime;
- }
- }