java經典50編程題


  1. 菲波拉契數列:有一對兔子,從出生后第 3 個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?
     1 package com.day2;
     2 public class test1 {
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         int s1 = 1, s2 = 1, s, month = 24;
     6         System.out.println("第1個月的兔子總數:\t"+1);
     7         System.out.println("第2個月的兔子總數:\t"+1);
     8         for(int i = 3; i <= month; i++)
     9         {
    10             //每個月的兔子總數是前兩個月的總和
    11             s = s2;
    12             s2 += s1;
    13             s1 = s;
    14             System.out.println("第"+i+"個月的兔子總數:\t"+s2);
    15         }
    16         
    17     }
    18 
    19 }
    test1

     

  2. 判斷 101-200 之間有多少個素數,並輸出所有素數。
     1 package com.day2;
     2 public class test2 {
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         int count = 0;
     6         for(int i = 101; i <= 200; i++) 
     7         {
     8             boolean b = false;
     9             for (int j = 2; j < Math.sqrt(i); j++) {
    10                 if(i%j == 0)
    11                 {
    12                     b = false;
    13                     break;
    14                 }
    15                 else
    16                 {
    17                     b = true;
    18                 }
    19             }
    20             if(b == true)
    21             {
    22                 count ++;
    23                 System.out.println(i);
    24             }
    25         }
    26         System.out.println("素數的總數為:"+count);
    27     }
    28 
    29 }
    test2

     

  3. 打印出所有水仙花數
     1 package com.day2;
     2 
     3 public class test3 {
     4 public static void main(String[] args) {
     5     int b1, b2, b3;
     6     for(int m=101; m<1000; m++) 
     7     {
     8         b3 = m / 100;
     9         b2 = m % 100 / 10;
    10         b1 = m % 10;
    11         if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m)
    12         {
    13             System.out.println(m+"是一個水仙花數"); 
    14         }
    15     }
    16 }
    17 }
    test3

     

  4. 將一個正整數分解質因數。例如:輸入 90,打印出 90=2*3*3*5
     1 程序分析:對 n 進行分解質因數,應先找到一個最小的質數 k,然后按下述步驟完成:
     2 (1)如果這個質數恰等於 n,則說明分解質因數的過程已經結束,打印出即可。
     3 (2)如果 n <> k,但 n 能被 k 整除,則應打印出 k 的值,並用 n 除以 k 的商,作為新的正整數
     4 你 n,重復執行第一步。
     5 (3)如果 n 不能被 k 整除,則用 k+1 作為 k 的值,重復執行第一步。
     6 package com.day2;
     7 import java.util.*;
     8 public class test4 {
     9     public static Scanner input = new Scanner(System.in);
    10     public static void main(String[] args) {
    11         // TODO Auto-generated method stub
    12         System.err.println("請輸入一個數:");
    13         int x = input.nextInt();
    14         System.out.print(x+"= ");
    15         int i = 2;
    16         while(i <= x){    //使用循環來找到可以被整除的數,然后通過out函數輸出
    17             if(i == x)//如果相等的話,就說明這個數沒有因數,只有1和它自己;
    18             {
    19                 System.out.println(i);
    20                 break;
    21             }
    22             else if(x % i ==0)//如果這個數有因數,然后找到除去這個因數后的值,繼續循環
    23             {
    24                 System.out.print(i+"*");
    25                 x = x / i;
    26             }
    27             else    //如果都不滿足,則繼續循環,
    28             {
    29                 i++;
    30             }
    31         }
    32     }
    33 
    34 }
    test4

     

  5. 三目運算符:利用條件運算符的嵌套來完成此題:學習成績> =90 分的同學用 A 表示,60-89 分之間的用 B 表示,60 分以下的用 C 表示。

     1 package com.day2;
     2 import java.util.*;
     3 public class test5 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         System.err.println("請輸入一個成績:");
     8         int x = input.nextInt();
     9         char grade = (x >= 90) ? 'A'
    10                     :(x >= 60) ? 'B'
    11                     : 'C';
    12         System.out.println("該學生的成績水平是:\t"+grade);        
    13     }
    14 }
    test5

     

  6. 輸入兩個正整數 m 和 n,求其最大公約數和最小公倍數
     1 /**在循環中,只要除數不等於 0,用較大數除以較小的數,將小的一個數作為下一輪循環的
     2 大數,取得的余數作為下一輪循環的較小的數,如此循環直到較小的數的值為 0,返回較大
     3 的數,此數即為最大公約數,最小公倍數為兩數之積除以最大公約數。* /
     4 package com.day2;
     5 import java.util.*;
     6 public class test6 {
     7     public static Scanner input = new Scanner(System.in);
     8     public static void main(String[] args) {
     9         System.out.println("請輸入兩個數來求最大公約數和最小公倍數:");
    10         System.out.println("第一個數:");
    11         int a = input.nextInt();
    12         System.out.println("第二個數:");
    13         int b = input.nextInt();
    14         Deff cd = new Deff();
    15         int x = cd.deff(a, b);//調用函數找到最大公約數
    16         int y = a * b / x;//兩個數的積除以最大公約數就是最小公倍數
    17         System.out.println("最大公約數為:"+x);
    18         System.out.println("最小公倍數為:"+y);
    19     }
    20 
    21 }
    22 class Deff{
    23     public int deff(int a, int b)
    24     {
    25         if(a < b)
    26         {
    27             a = a ^ b;
    28             b = a ^ b;
    29             a = a ^ b;
    30         }
    31         while(b != 0)
    32         {
    33             if(a == b)
    34                 return a;
    35             else
    36             {
    37                 int k = a % b;
    38                 a = b ;
    39                 b = k;
    40             }
    41         }
    42         return a;
    43     }
    44 }
    test6

     

  7. 輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數
     1 package com.day2;
     2 import java.util.*;
     3 public class test7 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         System.out.println("請輸入一行字符串:");
     7         String str = input.nextLine();
     8         int digital = 0,character = 0, other = 0, blank = 0;
     9         char [] ch = str.toCharArray();//String的方法,將字符串轉換為字符數組;
    10         for (int i = 0; i <ch.length; i++) {
    11             if(ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z')
    12                 character++;
    13             else if(ch[i] >= '0' && ch[i] <= '9')
    14                 digital++;
    15             else if(ch[i] == ' ')
    16                 blank++;
    17             else
    18                 other++;
    19         }
    20         System.out.println("字母個數:"+character);
    21         System.out.println("數字個數:"+digital);
    22         System.out.println("空格個數:"+blank);
    23         System.out.println("其他個數:"+other);
    24     }
    25 }
    test7

     

  8. 求 s=a+aa+aaa+aaaa+aa...a 的值,其中 a 是一個數字。例如 2+22+222+2222+22222(此時共有 5 個數相加),幾個數相加由鍵盤控制

     1 package com.day2;
     2 import java.util.*;
     3 public class test8 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         System.out.println("請輸入個位數字:");
     7         int single = input.nextInt();
     8         System.out.println("請輸入最高位數:");
     9         int max = input.nextInt();
    10         int sum = 0,temp = 0;
    11         for (int i = 0; i < max; i++) {
    12             temp = single + temp;    //先把本次要加的值賦值給temp;
    13             single *= 10;            //每次把單數乘以10,向前進一位,加上之前的temp正好滿足需要
    14             sum = sum + temp;        //把每次的temp相加起來就是要的結果
    15         }
    16         System.out.println("數字"+single+"公共有"+max+"個數相加的好結果為:"+sum);
    17     }
    18 }
    test8

     

  9. 一個數如果恰好等於它的因子之和,這個數就稱為 "完數 "。例如 6=1+2+3.編程找出 1000 以內的所有完數

     1 package com.day2;
     2 public class test9 {
     3     public static void main(String[] args) {
     4         for (int i = 1; i <= 1000; i++) {
     5             int b = 0;//每次都要把b重置
     6             for (int j = 1; j <= i/2; j++) {
     7                 if(i % j == 0)//找到因數,然后相加
     8                 {
     9                     b = b + j;//相加供后邊使用
    10                 }
    11             }
    12             if(i == b)//如果是完數,則輸出完數
    13                 System.out.println(i);
    14         }
    15     }
    16 }
    test9

     

  10. 一球從 100 米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10 次落地時,共經過多少米?第 10 次反彈多高?

     1 package com.day2;
     2 import java.util.*;
     3 public class test10 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         System.out.println("請輸入第幾次?");
     7         int num = input.nextInt();
     8         double sum = 0, high = 100;
     9         for (int i = 1; i < num; i++) {
    10             if(i == 1)
    11                 sum += high;
    12             else
    13                 sum = sum + 2*high;
    14             if(i < 10)
    15                 high /= 2;
    16         }
    17         System.out.println("第"+num+"次時經過"+sum+"米,第"+num+"次反彈"+high+"米!");
    18     }
    19 }
    test10
     1 package com.day2;
     2 import java.util.*;
     3 public class test10 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         System.out.println("請輸入第幾次?");
     7         int num = input.nextInt();
     8         double sum = 100, high = 100;
     9         for (int i = 1; i < num; i++) {//執行9次
    10                 sum += high;
    11                 high /= 2;
    12         }
    13         System.out.println("第"+num+"次時經過"+sum+"米,第"+num+"次反彈"+high+"米!");
    14     }
    15 }
    test10

     

  11. 有 1、 2、 3、 4 四個數字, 能組成多少個互不相同且無重復數字的三位數?都是多少?
     1 package com.day3;
     2 public class test11 {
     3     public static void main(String[] args) {
     4         int count = 0 ;
     5         for (int i = 1; i < 5; i++) {//最外層循環,控制百位數;
     6             for (int j = 1; j < 5; j++) {//第二層循環控制十位數;
     7                 for (int z =1; z < 5; z++) {//第三層循環控制個位數;
     8                     if(i!=j&&i!=z&&j!=z)//如果三個位上的值互不相等,執行計數操作;
     9                     {
    10                         count++;
    11                         System.out.println(i*100+j*10+z);
    12                     }
    13                 }
    14             }
    15         }
    16         System.out.println("共有"+count+"個這樣的數!");
    17     }
    18 }
    test11

     

  12. 企業發放的獎金根據利潤提成。利潤(I)低於或等於 10 萬元時,獎金可提 10%;利潤高於 10 萬元,低於 20 萬元時,低於 10 萬元的部分按 10%提成,高於 10 萬元的部分,可可提成 7.5%;20 萬到 40 萬之間時,高於 20 萬元的部分,可提成 5%;40 萬到 60 萬之間時高於 40 萬元的部分, 可提成 3%; 60 萬到 100 萬之間時, 高於 60 萬元的部分, 可提成 1.5%,高於 100 萬元時,超過 100 萬元的部分按 1%提成,從鍵盤輸入當月利潤,求應發放獎金總數?

     1 package com.day3;
     2 import java.util.*;
     3 public class test12 {
     4     public static void main(String[] args) {
     5         double x = 0,y = 0;
     6         System.out.print("輸入當月利潤(萬) :");
     7         Scanner s = new Scanner(System.in);
     8         x = s.nextInt();
     9         if(x > 0 && x <= 10) {
    10             y = x * 0.1;
    11         } else if(x > 10 && x <= 20) {
    12             y = 10 * 0.1 + (x - 10) * 0.075;
    13         } else if(x > 20 && x <= 40) {
    14             y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;
    15         } else if(x > 40 && x <= 60) {
    16             y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03;
    17         } else if(x > 60 && x <= 100) {
    18             y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015;
    19         } else if(x > 100) {
    20             y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01;
    21         }
    22         System.out.println("應該提取的獎金是 " + y + "萬");
    23     }
    24 }
    test12

     

  13. 一個整數,它加上 100 后是一個完全平方數,再加上 168 又是一個完全平方數,請問該數是多少?

     1 package com.day3;
     2 public class test13 {
     3     public static void main(String[] args) {
     4         long startTime = System.currentTimeMillis(); //獲取執行開始時間
     5         int i = 0;
     6         while(true)
     7         {
     8             if(Math.sqrt(i+100) % 1 == 0)
     9                 if(Math.sqrt(i+100+168) % 1 ==0)
    10                 {
    11                     System.out.println(i+"加上100或者168都是完全平方數!");
    12                 }
    13             i++;
    14             if(i > 10000)
    15                 break;
    16         }
    17         long endTime = System.currentTimeMillis(); //獲取執行結束時間
    18         System.out.println("time:" + (endTime - startTime));  //打印程序執行時間
    19     }
    20 }
    21 
    22 
    23 程序也可參考以下地址的寫法:http://blog.csdn.net/yueqinglkong/article/details/22805293
    test13

     

  14. 輸入某年某月某日,判斷這一天是這一年的第幾天?
     1 package com.day3;
     2 import java.util.Scanner;
     3 public class test14 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         int day , month ,year , dayNum = 0;//定義年月日,以及本月之前的總天數
     7         while(true)
     8         {
     9             System.out.println("請輸入年:");
    10             year = input.nextInt();
    11             System.out.println("請輸入月:");
    12             month = input.nextInt();
    13             System.out.println("請輸入日:");
    14             day = input.nextInt();            
    15             if(month < 1 || month > 12 || day < 1 || day > 31)
    16                 continue;
    17             else
    18                 break;
    19         }
    20         for(int i =1; i < month; i++)//通過循環來找到本月之前的總天數;判斷月的總天數和閏年等
    21         {
    22             int days = 0;
    23             switch(i)
    24             {
    25             case 1:
    26             case 3:
    27             case 5:
    28             case 7:
    29             case 8:
    30             case 10:
    31             case 12:
    32                     days = 31;
    33                     break;
    34             case 4:
    35             case 6:
    36             case 9:
    37             case 11:
    38                     days = 30;
    39                     break;
    40             case 2://閏年29天,非閏年28天
    41                     if(year % 400 ==0||(year%4 == 0 && year % 100 != 0))
    42                         days = 29;
    43                     else
    44                         days = 28;
    45                     break;
    46             }
    47             dayNum += days;//輸入月份之前月份的總天數
    48         }
    49         System.out.println("這是本年的第"+(dayNum+day)+"天");
    50     }
    51 }
    test14

     

  15. 輸入三個整數 x,y,z,請把這三個數由小到大輸出。
     1 package com.day3;
     2 import java.util.Arrays;
     3 import java.util.Scanner;
     4 public class test15 {
     5     public static Scanner input = new Scanner(System.in);
     6     public static void main(String[] args) {
     7         long startTime = System.currentTimeMillis(); //獲取執行開始時間
     8         sort(44,35,37);
     9         long endTime = System.currentTimeMillis(); //獲取執行結束時間
    10         System.out.println("time:" + (endTime - startTime));  //打印程序執行時間
    11     }
    12     public static void sort(int a, int b, int c)
    13     {
    14         if(a > b)
    15         {
    16             a = a ^ b ;
    17             b = a ^ b ;
    18             a = a ^ b ;
    19         }
    20         if(a > c)
    21         {
    22             a = a ^ c ;
    23             c = a ^ c ;
    24             a = a ^ c ;
    25         }
    26         if(b > c)
    27         {
    28             b = b ^ c ;
    29             c = b ^ c ;
    30             b = b ^ c ;
    31         }
    32         System.out.println("從小到大的順序是:"+a+"<"+b+"<"+c);
    33     }
    34     public static void sort1(int a, int b, int c)
    35     {
    36         int arr[]  = {a,b,c};
    37         Arrays.sort(arr);
    38         System.out.println("從小到大依次是:");
    39         for (int i = 0; i < arr.length; i++) {
    40             System.out.print(arr[i]);
    41             System.out.print(' ');
    42         }
    43     }
    44 }
    test15

     

  16. 輸出 9*9 口訣。
     1 package com.day4;
     2 public class test16 {
     3     public static void main(String[] args) {
     4         for (int i = 1; i < 10 ; i++) {
     5             for (int j = 1; j <= i ; j++) {
     6                 System.out.print(j+"*"+i+"="+i*j+"\t");
     7             }
     8             System.out.println();
     9         }
    10     }
    11 }
    test16

     

  17. 猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下 的一半零一個。 到第10天早上想再吃時, 見只剩下一個桃子了。 求第一天共摘了多少。

     1 package com.day4;
     2 public class test17 {
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         int num = 1;
     6         for (int i = 9; i >= 1; i--) {
     7             num = (num + 1) * 2;
     8         }
     9         System.out.println("猴子第一天摘的桃子的個數是:"+num);
    10     }
    11 
    12 }
    test17

     

  18. 兩個乒乓球隊進行比賽,各出三人。甲隊為 a,b,c 三人,乙隊為 x,y,z 三人。已抽簽決定比賽名單。有人向隊員打聽比賽的名單。a 說他不和 x 比,c 說他不和 x,z 比,請編程序找出三隊賽手的名單。

     1 package com.day4;
     2 public class test18 {
     3     static  char[] m ={'a','b','c'};//把要處理的字符放進字符數組中便於處理;
     4     static  char[] n ={'x','y','z'};
     5     public static void main(String[] args) {
     6         for (int i = 0; i < m.length; i++) {//外層循環遍歷甲隊隊員,
     7             for (int j = 0; j < n.length; j++) {//內層循環遍歷乙隊隊員,
     8                 if(m[i] == 'a' && n[j] == 'x')
     9                     continue;
    10                 //根據題意知道c對戰y,a不可能對戰y;
    11                 else if(m[i] == 'a' && n[j] == 'y')
    12                     continue;
    13                 //根據題意;
    14                 else if((m[i] == 'c' && n[j] == 'x' ) || (m[i] == 'c' && n[j] == 'z'))
    15                     continue;
    16                 //推測出b不可能對戰y和z;
    17                 else if((m[i] == 'b' && n[j] == 'y' ) || (m[i] == 'b' && n[j] == 'z'))
    18                     continue;
    19                 else
    20                     System.out.println(m[i]  +"對戰"+n[j]);
    21             }
    22         }
    23     }
    24 }    
    test18

     

  19. 打印出如下圖案(菱形)
     1 package com.day4;
     2 import java.util.*; 
     3 public class test19 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         System.out.println("請輸入你要顯示的總行數(奇數):");
     7         int num = input.nextInt();
     8         for (int i = 1; i <= (num+1) / 2; i++) {//此循環是控制上層的三角的,包括最中間的一行;
     9             for (int j = 0; j < (num+1) / 2 -i ; j++) {//控制每一行的空格數
    10                 System.out.print(" ");
    11             }
    12             for (int j = 0; j < 2*i - 1; j++) {//控制每一行顯示的*符號數
    13                 System.out.print("*");
    14             }
    15             System.out.println();//換行
    16         }
    17         for (int i = 1; i <= (num -1 ) / 2; i++) {//此循環是控制下層的三角的
    18             for (int j = 0; j < i ; j++) {//控制每一行的空格數
    19                 System.out.print(" ");
    20             }
    21             for (int j = 0; j < num - 2*i; j++) {//控制每一行顯示的*符號數
    22                 System.out.print("*");
    23             }
    24             System.out.println();//換行
    25         }
    26     }
    27 }
    test19

     

  20. 有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前 20 項之和
     1 package com.day4;
     2 import java.util.Scanner;
     3 public class test20 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         int x = 2 , y = 1;
     7         double sum = 0;
     8         for (int i = 1; i <= 20; i++) {//根據之間的規律來逐項想加
     9             sum = sum + (double)x / y;
    10             x = x ^ y;
    11             y = x ^ y;
    12             x = x ^ y;
    13             x = x + y;
    14         }
    15         System.out.println("前20項想加之和為:"+sum);
    16     }
    17 
    18 }
    test20

     

  21. 求 1+2!+3!+...+20!的和
     1 package day5;
     2 public class test21 {
     3     public static void main(String[] args) {
     4         long sum = 0 ;long temp = 1;//必須要設置為long類型,不然超過范圍;
     5         for (int i = 1; i <= 20; i++) {
     6             temp = 1;
     7             for (int j = 1; j <= i; j++) {
     8                 temp *= j;
     9             }
    10             sum += temp;
    11         }
    12         System.out.println(sum);
    13     }
    14 }
    test21

     

  22. 利用遞歸方法求 5!;
     1 package day5;
     2 public class test12 {
     3     public static void main(String[] args) {
     4         System.out.println(rec(5));
     5     }
     6     public static long rec(int n) {//定義函數實現遞歸
     7         long value = 0 ;
     8         if(n ==1 ) 
     9         {
    10             value = 1;
    11         } 
    12         else
    13         {
    14             value = n * rec(n-1);
    15         }
    16         return value;
    17     }
    18 }
    test22

     

  23. 有 5 個人坐在一起,問第五個人多少歲?他說比第 4 個人大 2 歲。問第 4 個人歲數,他說比第 3 個人大 2 歲。問第三個人,又說比第 2 人大兩歲。問第 2 個人,說比第一個人大兩歲。最后問第一個人,他說是 10 歲。請問第五個人多大?

     1 package day5;
     2 public class test23 {
     3     public static void main(String[] args) {
     4         int age = 10;//第一個人的年齡
     5         for (int i = 1; i <= 4; i++) {//依次從第一個人加到第五個人
     6             age += 2;
     7         }
     8         System.out.println("第五個人"+age+"歲");
     9     }
    10 }
    test23

     

  24. 給一個不多於 5 位的正整數, 要求: 一、 求它是幾位數, 二、 逆序打印出各位數字。

     1 package day5;
     2 import java.util.Scanner;
     3     
     4 public class test24 {
     5     public static Scanner input = new Scanner(System.in);
     6     public static void main(String[] args) {
     7         System.out.println("請輸入一個不多於五位數的數字:");
     8         Integer num = input.nextInt();//定義Integer類型變量,便於轉換成數組;
     9         String numString = Integer.toString(num);//利用Integer的方法轉換成字符串;
    10         char [] arrChar = numString.toCharArray();//利用字符串的方法轉換成字符數組,便於求長度和輸出
    11         System.out.println("您輸入的是"+arrChar.length+"位數");
    12         for (int i = 0; i < arrChar.length; i++) {
    13             System.out.println("第"+(i+1)+"個數字是"+arrChar[i]);
    14         }
    15         System.out.println("逆序打印:");
    16         for (int i = arrChar.length - 1; i >= 0; i--) {
    17             System.out.print(arrChar[i]);
    18         }
    19     }
    20 }
    test24  
  25. 一個 5 位數,判斷它是不是回文數。即 12321 是回文數,個位與萬位相同,十位與千位相同。

    test25
     1 package day5;
     2 import java.util.Scanner;
     3 public class test25_1 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         boolean isHuiWen = false;
     7         System.out.println("請輸入一個數是不是回文數:");
     8         Integer num = input.nextInt();
     9         char[] arrChar = num.toString().toCharArray();//像上一題一樣,利用字符數組解決
    10         for (int i = 0; i < arrChar.length / 2; i++) {
    11             if (arrChar[i] == arrChar[arrChar.length - i - 1]) {
    12                 isHuiWen = true;
    13             }else {
    14                 isHuiWen = false;
    15             }
    16         }
    17         if (isHuiWen) {
    18             System.out.println("這個數是回文數!");
    19         }else {
    20             System.out.println("這個數不是回文數!");
    21         }
    22     }
    23 }
    不限制位數

     

  26. 請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續 判斷第二個字母。

     1 package com.day6;
     2 import java.util.Scanner;
     3 public class test26 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         System.out.println("請輸入一個字符串:");
     7         String str = input.nextLine().toUpperCase();//將輸入的都轉換成大寫
     8         switch(str.charAt(0))//利用字符串的charAt方法,取得字符串的第一個字符
     9         {
    10             case 'M':
    11                 System.out.println("Monday");
    12                 break;
    13             case 'W':
    14                 System.out.println("Wednesday");
    15                 break;
    16             case 'F':
    17                 System.out.println("Friday");
    18                 break;
    19             case 'T': {//利用字符串的charAt方法,取得字符串的第二個字符
    20                 if(str.charAt(1)== 'U') {System.out.println("Tuesday"); }
    21                 else if(str.charAt(1)== 'H') {System.out.println("Thursday"); }
    22                 else {System.out.println("無此寫法!");
    23             }
    24             };
    25             break;
    26             case 'S': {
    27             if(str.charAt(1) == 'U') {System.out.println("Sunday"); }
    28             else if(str.charAt(1) == 'A') {System.out.println("Saturday"); }
    29             else {System.out.println("無此寫法!");
    30             }
    31             };
    32             break;
    33             default:System.out.println("無此寫法!");
    34             }
    35         }
    36     }
    test26

     

     

  27. 求 100 之內的素數
     1 package com.day6;
     2 
     3 public class Test27 {
     4     public static void main(String[] args) {
     5         System.out.print("2 3 ");
     6         boolean is = false;
     7         for (int i = 4; i <= 100; i++) {
     8             for (int j = 2; j <= Math.sqrt(i); j++) {
     9                 if(i % j == 0)//不是素數,找下一個數
    10                 {
    11                     is = false;
    12                     break;
    13                 }
    14                 else//是素數,設為true;輸出此數
    15                     is = true;
    16             }
    17             if (is == true) {
    18                 System.out.print(i+" ");
    19             }
    20         }
    21     }
    22 }
    Test27

     

  28. 對 10 個數進行排序
     1 package com.day6;
     2 
     3 import java.lang.reflect.Array;
     4 import java.util.Arrays;
     5 import java.util.Scanner;
     6 public class Test28 {
     7     public static Scanner input = new Scanner(System.in);
     8     public static void main(String[] args) {
     9         System.out.println("請輸入您要輸入的個數");
    10         int num = input.nextInt();
    11         int [] arrInt = new int[num];
    12         System.out.println("輸入"+num+"位數進行排序:");
    13         for (int i = 0; i < num; i++) {
    14             arrInt[i] = input.nextInt();
    15         }
    16         //Arrays.sort(arrInt);//利用自帶的排序函數進行排序
    17         sort(arrInt);//自定義函數進行排序
    18         for (int i = 0; i < arrInt.length; i++) {
    19             System.out.println(arrInt[i]);
    20         }
    21     }
    22     public static int[] sort(int [] arr)
    23     {
    24         for (int i = 0; i < arr.length; i++) {
    25             for (int j = i; j < arr.length; j++) {
    26                 if(arr[i] > arr[j])
    27                 {
    28                     arr[i] = arr[i] ^ arr[j];
    29                     arr[j] = arr[i] ^ arr[j];
    30                     arr[i] = arr[i] ^ arr[j];
    31                 }
    32             }
    33         }
    34         return arr;
    35     }
    36 }
    Test28

     

  29. 求一個 3*3 矩陣對角線元素之和
     1 package com.day6;
     2 import java.util.Scanner;
     3 public class Test29 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         int sum = 0;
     7         System.out.println("請輸入9個整數以求對角線之和");
     8         int [][] arrInt = new int[3][3];
     9         for (int i = 0; i < arrInt.length; i++) {
    10             for (int j = 0; j < arrInt.length; j++) {
    11                 arrInt[i][j] = input.nextInt();
    12             }
    13         }
    14         System.out.println("您輸入的9位數矩陣為:");
    15         for (int i = 0; i < arrInt.length; i++) {
    16             for (int j = 0; j < arrInt.length; j++) {
    17                 System.out.print(arrInt[i][j]+" ");
    18             }
    19             System.out.println();
    20         }
    21         for (int i = 0; i < arrInt.length; i++) {
    22             for (int j = 0; j < arrInt.length; j++) {
    23                 if (i == j || i == arrInt.length - 1 - j ) {
    24                     sum += arrInt[i][j];
    25                 }
    26                 if (i ==  1 && j == 1) {//最中間的那個數少加一次,要記得加上,如果不是9位矩陣,則需改變
    27                     sum += arrInt[i][j];
    28                 }
    29             }
    30         }
    31         System.out.println("對角線之和為:"+sum);
    32     }
    33 }
    Test29

     

  30. 有一個已經排好序的數組。現輸入一個數,要求按原來的規律將它插入數組中。

     1 package com.day6;
     2 import java.util.Scanner;
     3     public class Test30 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         int[] a = new int[]{1, 2, 6, 14, 25, 36, 37,55};
     7         int[] b = new int[a.length+1];
     8         int i =0;
     9         System.out.print("請輸入一個整數:");
    10         int num = input.nextInt();
    11         if(num >= a[a.length-1]) 
    12         {//如果大於最大數,直接加在最后
    13             b[b.length-1] = num;
    14             for(i=0; i<a.length; i++)
    15             {//把a數組復制給b數組
    16                 b[i] = a[i];
    17             }
    18         } 
    19         else 
    20         {//如果不大於最大數
    21             for(i=0; i<a.length; i++)
    22             {
    23                 if(num >= a[i]) 
    24                 {//如果次數大於當前的數
    25                     b[i] = a[i];//加在b對應的的位置
    26                 } 
    27                 else 
    28                 {
    29                     b[i] = num;
    30                     break;
    31                 }
    32             }
    33             for(int j=i+1; j<b.length; j++) 
    34             {//a中的i后邊元素都在b中往后移一個位置
    35                 b[j] = a[j-1];
    36             }
    37         }
    38         for (i = 0; i < b.length; i++) 
    39         {//輸出數組
    40             System.out.print(b[i] + " ");
    41         }
    42     }
    43 }
    Test30

     

  31. 將一個數組逆序輸出 

     1 package com.day7;
     2 import java.util.Scanner;
     3 public class Test31 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         int [] arr = new int [100];//初始化定義數組,默認長度為100;        
     7         System.out.println("請輸入多個正整數(輸入-1結束):");
     8         int i = 0;//定義i是為了知道數組中有多少個元素;
     9         do//用戶do while循環是為了控制數組輸入的結束;
    10         {
    11             arr[i] = input.nextInt();
    12             i++;
    13         }while(arr[i-1] != -1);//第一次到這里的時候,i已經是1,所以可以減去1
    14         System.out.println("您輸入的數組是:");
    15         for (int j2 = 0; j2 < i-1; j2++) {//順序輸入剛才輸入的數組
    16             System.out.println(arr[j2]+ " ");
    17         }
    18         System.out.println("您輸入的數組逆序輸出為:");
    19         for (int j2 = 0; j2 < i-1; j2++) {//逆序輸入剛才輸入的數組
    20             System.out.println(arr[i-2-j2] + " ");
    21         }
    22     }
    23 }
    Test31

     

  32. 取一個整數 a 從右端開始的 4~7 位
     1 package com.day7;
     2 import java.util.Scanner;
     3 public class Test32 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         System.out.println("請輸入一個大於7位數的整數:");
     7         long num = input.nextLong();//定義數值類型是long類型,防止越界
     8         String str = Long.toString(num);//將long類型轉換成字符串
     9         char[] charStr = str.toCharArray();//利用字符串的方法轉換為字符數組
    10         int length = charStr.length;
    11         if (length < 7) {//容錯判斷
    12             System.out.println("您輸入的整數長度有誤!");
    13         }
    14         else {//如果輸入正確,輸入該整數的倒數4-7位
    15             System.out.println("您輸入的整數從右端開始的4-7位分別是:"+
    16                     charStr[length-4] +" "+charStr[length-5]+" "
    17                     +charStr[length-6]+" "+charStr[length-7]);
    18         }
    19     }
    20 }
    Test32

     

  33. 打印出楊輝三角形(手動選擇要打印的行數)
     1 package com.day7;
     2 import java.util.Scanner;
     3 public class Test33 {
     4     public static void main(String[] args) {
     5         Scanner input = new Scanner(System.in);
     6         System.out.println("請輸入要顯示的楊輝三角的行數:");
     7         int num = input.nextInt();//獲得要顯示的行數
     8         int[][] arr = new int[num][num];//定義二維數組,存儲要顯示的數字
     9         for (int i = 0; i < arr.length; i++) {
    10             arr[i][i] = 1;//確定每行最后的數字
    11             arr[i][0] = 1;//確定每行開始的數字
    12         }
    13         for (int i = 2; i < arr.length; i++) 
    14         {//獲取每一行的開始和結束的數字
    15             for (int j = 1; j < i; j++) {
    16                 arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
    17             }
    18         }
    19         for (int i = 0; i < arr.length; i++) 
    20         {//打印出二維數組
    21             for (int j = 0; j < 2*(arr.length-i)-1; j++) 
    22             {//控制每一行的最前面顯示的空格數
    23                 System.out.print(" ");
    24             }
    25             for (int j = 0; j <= i; j++) 
    26             {//打印出數組中的元素,並且以空格隔開
    27                 System.out.print(arr[i][j]+"   ");
    28             }
    29             System.out.println();//每次打印一行結束之后換行;
    30         }
    31         
    32     }
    33 }
    Test33

     

  34. 輸入 3 個數 a,b,c,按大小順序輸出。
     1 package com.day7;
     2 import java.util.Arrays;//引入Arrays,獲取排序方法
     3 import java.util.Scanner;
     4 public class Test34 {
     5     public static void main(String[] args) {
     6         Scanner input = new Scanner(System.in);
     7         int [] arr = new int[3];
     8         System.out.println("請輸入三個數字,以按照大小輸出:");
     9         for (int i = 0; i < arr.length; i++) {
    10             arr[i] = input.nextInt();
    11         }
    12         Arrays.sort(arr);//利用JAVA數組的排序,直接輸出數組
    13         for (int i = 0; i < arr.length; i++) {
    14             System.out.println(arr[i]);
    15         }
    16     }
    17 }
    Test34

     

  35.  輸入數組, 最大的與第一個元素交換, 最小的與最后一個元素交換, 輸出數組。
     1 package com.day7;
     2 import java.util.Scanner;
     3 public class Test35 {
     4     public static Scanner input = new Scanner(System.in);
     5     public static void main(String[] args) {
     6         System.out.println("請輸入要多大的數組:");
     7         int arrLength = input.nextInt();
     8         int [] arr  = new int[arrLength];
     9         for(int i = 0; i < arrLength; i++)
    10         {
    11             arr[i] = input.nextInt();//初始化數組
    12         }
    13         int max = arr[0] , min=arr[0] ,maxIndex = 0,minIndex = 0;
    14         for (int i = 1; i < arr.length; i++) {
    15             if (max < arr[i]) {//找到數組的最大值索引
    16                 max = arr[i];
    17                 maxIndex = i;
    18             }
    19             else if(min > arr[i]) {//找到數組的最小值索引
    20                 min = arr[i];
    21                 minIndex = i;
    22             }
    23         }
    24         if(maxIndex != 0)//如果最大值的索引不是0,交換元素
    25         {
    26             arr[0] = arr[0] ^ arr[maxIndex];
    27             arr[maxIndex] = arr[0] ^ arr[maxIndex];
    28             arr[0] = arr[0] ^ arr[maxIndex];
    29         }
    30         if(minIndex != arrLength - 1 )//如果最大值的索引不是arrLength - 1,交換元素
    31         {    
    32             arr[arrLength - 1] = arr[arrLength - 1] ^ arr[minIndex];
    33             arr[minIndex] = arr[arrLength - 1] ^ arr[minIndex];
    34             arr[arrLength - 1] = arr[arrLength - 1] ^ arr[minIndex];
    35         }
    36         for (int i = 0; i < arr.length; i++) {//輸出數組
    37             System.out.println(arr[i]);
    38         }
    39     }
    40 }
    Test35

     

  36. 有n個整數, 使其前面各數順序向后移m個位置, 最后m個數變成最前面的m個數
     1 package com.day8;
     2 import java.util.Scanner;
     3 public class Test36 {
     4     public static void main(String[] args) {
     5         Scanner input = new Scanner(System.in);
     6         System.out.println("請輸入數組的長度:");//定義數組長度
     7         int num = input.nextInt();
     8         int [] arr = new int[num];
     9         System.out.println("請輸入數組元素:");//鍵入數組元素
    10         for (int i = 0; i < num; i++) {
    11             arr[i] = input.nextInt();
    12         }
    13         System.out.println("您輸入的數組是:");//打印數組
    14         for (int j = 0; j < arr.length; j++) {
    15             System.out.print(arr[j] + " " );
    16         }
    17         System.out.println("請輸入移動的位數:");//獲取移動位數
    18         int m = input.nextInt();
    19         int [] arr2 = new int[num]; 
    20         for (int k = 0; k < m; k++) {//先把移動的轉移進新數組
    21             arr2[k] = arr[num - m + k];
    22         }
    23         for (int k2 = 0; k2 < num - m; k2++) {//把向后移的插入到新數組
    24             arr2[m+k2] = arr[k2];
    25         }
    26         System.out.println("移動后的數組為:");
    27         for (int l = 0; l < arr2.length; l++) {
    28             System.out.println(arr2[l]);
    29         }
    30     }
    31 }
    Test36

     

  37. 有 n 個人圍成一圈,順序排號。從第一個人開始報數(從 1 到 3 報數),凡報到 3的人退出圈子,問最后留下的是原來第幾號的那位

     1 package com.day8;
     2 import java.util.Scanner;
     3 public class Test37 {
     4     public static void main(String[] args) {
     5         Scanner input = new Scanner(System.in);
     6         System.out.println("請輸入總人數:");//定義數組長度
     7         int num = input.nextInt();
     8         //定義數組,用其中的元素標記是否已經被淘汰,0表示為被淘汰
     9         int [] arr = new int[num];
    10         for (int i = 0; i < num; i++) {//初始化數組元素都是1
    11             arr[i] = 1;
    12         }
    13         for (int i = 0; i < arr.length; i++) {
    14             System.out.println(arr[i]);
    15         }
    16         int index = 0;
    17         int sum = 0;
    18         while(num > 1 )//用來控制剩余的人數
    19         {
    20             if (arr[index] == 1) {
    21                 sum++;
    22                 if (sum == 3) {//如果是3,則重新記,從1開始
    23                     sum = 0; 
    24                     arr[index] = 0; 
    25                     num-- ;
    26                 }
    27             }
    28         index++ ;
    29         if (index == arr.length) {//如果索引是數組的長度,則從0開始
    30             index = 0 ;
    31         }
    32         }
    33         for (int i = 0; i < arr.length; i++) {
    34             System.out.println(arr[i]);
    35         }
    36         for (int i = 0; i < arr.length; i++) {
    37             if (arr[i] == 1) {
    38                 System.out.println("第"+(i+1)+"留了下來");
    39             }
    40         }
    41     }
    42 }
    Test37

     

     

  38. 寫一個函數, 求一個字符串的長度, 在 main 函數中輸入字符串, 並輸出其長度。
     1 package com.day8;
     2 import java.util.Scanner;
     3 public class Test38 {
     4     public static void main(String[] args) {
     5         Scanner input = new Scanner(System.in);
     6         System.out.println("請輸入一個字符串:");
     7         String str = input.nextLine();
     8         System.out.println("該字符串的長度是:"+getArrLength(str));
     9     }
    10     public static int getArrLength(String str)
    11     {
    12         char[] charStr = str.toCharArray();
    13         return charStr.length;
    14     }
    15 }
    Test38

     

  39. 編寫一個函數,輸入 n 為偶數時,調用函數求 1/2+1/4+...+1/n,當輸入 n 為奇數時,調用函數 1/1+1/3+...+1/n

     1 package com.day8;
     2 import java.util.Scanner;
     3 public class Test39 {
     4     public static void main(String[] args) 
     5     {
     6         Scanner s = new Scanner(System.in);
     7         System.out.print("請輸入一個正整數 n= ");
     8         int n = s.nextInt();
     9         System.out.println("相應數列的和為:" + sum(n));
    10     }
    11     public static double sum(int n) 
    12     {
    13         double res = 0;
    14         if(n % 2 == 0) {
    15             for(int i=2; i<=n; i+=2) {
    16         res += (double)1 / i;
    17         }
    18         } else {
    19         for(int i=1; i<=n; i+=2) {
    20         res += (double)1 / i ;
    21         }
    22         }
    23         return res;
    24     }
    25 }
    Test39

     

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM