一、 題目:打印出楊輝三角形(要求打印出10行以下圖)java
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
......
package com.jzq.test1; /** * 題目:打印出楊輝三角形(要求打印出10行以下圖) * 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 ....... */ public class question1 { public static void main(String[] args) { int[][] arr = new int[10][10]; //最左邊和最右邊的數爲1 for (int i = 0; i < 10; i++) { arr[i][i] = 1; arr[i][0] = 1; } //上面兩個數相加等於下面的數字 for (int i = 2; i < 10; i++) { for (int j = 1; j < i; j++) { arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } } //打印楊輝三角 for (int i = 0; i < 10; i++) { //打印空格 for (int j = 0; j < 2 * (10 - i) - 1; j++) { System.out.print(" "); } //打印數組中各個數字 for (int k = 0; k <= i; k++) { System.out.print(arr[i][k] + " "); } System.out.println(); } } }
二、題目:輸入3個數a,b,c,按大小順序輸出。編程
package com.jzq.test1; import java.util.Scanner; /** * 題目:輸入3個數a,b,c,按大小順序輸出。 */ public class question2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入3個整數"); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); if (a < b) { int temp = a; a = b; b = temp; } if (a < c) { int temp = a; a = c; c = temp; } if (b < c) { int temp = b; b = c; c = temp; } System.out.println("從大到小的順序輸出"); System.out.println(a + " " + b + " " + c); } }
三、題目:有一、二、三、4四個數字,能組成多少個互不相同且無重複數字的三位數?都是多少?數組
package com.jzq.test1; import org.junit.Test; import java.util.Scanner; /** * 題目:有一、二、三、4四個數字,能組成多少個互不相同且無重複數字的三位數?都是多少? */ public class question3 { @Test public void test1() { int count = 0; for (int x = 1; x < 5; x++) { for (int y = 1; y < 5; y++) { for (int z = 1; z < 5; z++) { if (x != y && y != z && x != z) { count++; System.out.println(x * 100 + y * 10 + z); } } } } System.out.println("共有" + count + "個三位數"); }
四、題目:輸入某年某月某日,判斷這一天是這一年的第幾天?spa
package com.jzq.test1; import java.util.Scanner; /** * 題目:輸入某年某月某日,判斷這一天是這一年的第幾天? */ public class question4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入年,月,日"); System.out.println("年:"); int year = scanner.nextInt(); System.out.println("月:"); int month = scanner.nextInt(); System.out.println("日:"); int day = scanner.nextInt(); int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int totalDay = 0; if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) { months[1] += 1; } if (month == 1) { System.out.println(year + "年" + month + "月" + day + "日是這一年的第" + day + "天"); } else { for (int i = 0; i < month - 1; i++) { totalDay += months[i]; } totalDay += day; System.out.println(year + "年" + month + "月" + day + "日是這一年的第" + totalDay + "天"); } } }
五、題目:判斷101-200之間有多少個素數,並輸出全部素數。code
package com.jzq.test1; /** * 題目:判斷101-200之間有多少個素數,並輸出全部素數。 */ public class question5 { public static void main(String[] args) { int count = 0; for (int i = 101; i < 200; i++) { if (isRightNum(i)) { System.out.print(i + " "); count++; } if (count % 10 == 0) { System.out.println(); } } System.out.println("素數的個數爲:" + count); } private static boolean isRightNum(int i) { for (int j = 2; j < Math.sqrt(i); j++) { if (i % j == 0) { return false; } } return true; } }
六、題目:一個數若是剛好等於它的因子之和,這個數就稱爲"完數"。例如6=1+2+3.編程找出1000 之內的全部完數。blog
package com.jzq.test1; /** * 題目:一個數若是剛好等於它的因子之和,這個數就稱爲"完數"。 * 例如6=1+2+3.編程找出1000 之內的全部完數。 */ public class question6 { public static void main(String[] args) { for(int i=1;i<1000;i++){ int sum =0; for(int j=1;j<(i/2+1);j++){ sum += j; if(sum == i){ System.out.println(i + "是完數"); } } } } }
七、題目:一球從1000 米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在第10 次落地時,共通過多少米?第10次反彈多高?遞歸
package com.jzq.test1; /** * 題目:一球從100 米高度自由落下,每次落地後反跳回原高度的一半; * 再落下,求它在第10 次落地時,共通過多少米?第10次反彈多高? */ public class question7 { public static void main(String[] args) { float h = 1000; float n = 10; float sum = h; h /= 2;//第一次下落,彈回到最高點 for (int i = 2; i <= n; i++) { //i從2開始,是由於在外層已經計算了第一次返回的h sum += h * 2; h /= 2; } System.out.println("總路徑是" + sum + "通過10次後的高度" + h); } }
八、 題目:輸入兩個正整數m 和n,求其最大公約數和最小公倍數。it
package com.jzq.test1; import java.util.Scanner; /** * 題目:輸入兩個正整數m 和n,求其最大公約數和最小公倍數。 */ public class question8 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入兩個數字:"); int m1 = scanner.nextInt(); int n1 = scanner.nextInt(); int m = m1 > n1 ? m1 : n1; int n = m1 < n1 ? m1 : n1; int sum = m * n; while (n != 0) { int temp = m % n; m = n; n = temp; } System.out.println("最大公約數" + m); System.out.println("最小公倍數" + sum / m); } }
九、題目:利用遞歸方法求5!。io
package com.jzq.test1; /** * 題目:利用遞歸方法求5!。 * 遞歸公式:f(n)=n*f(n-1) */ public class question9 { public static void main(String[] args) { int start = 5; int result = factorial(start); System.out.println(result); } public static int factorial(int i){ if(i==1){ return 1; } return i*factorial(i-1); } }
十、題目:給一個很少於5位的正整數,要求:1、求它是幾位數,2、正序打印出各位數字。class
package com.jzq.test1; import java.util.Scanner; /** * 題目:給一個很少於5位的正整數,要求:1、求它是幾位數,2、正序打印出各位數字。 */ public class question10 { public static void main(String[] args) { System.out.println("輸入一個很少於5位的正整數"); Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); int[] arr = new int[5]; int i = 0; do{ arr[i] = num % 10; num = num /10; i++; }while (num!=0); System.out.println("輸入數字串的是"+i+"位數的"); System.out.println("逆序輸出:"); for(int j = arr.length-1;j>=0;j--){ System.out.print(arr[j] + " "); } scanner.close(); } }