目錄:
非常經典的java編程題
程序1:斐波那契數列問題
題目概述:
古典問題: 有一對兔子,從出生第三個月起每月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數爲多少
代碼:
public class Programming1 {
//斐波那契數列問題
public static void main(String[] args) {
System.out.println(Fibonacci(1));
System.out.println(Fibonacci(2));
System.out.println(Fibonacci(3));
System.out.println(Fibonacci(4));
}
public static int function(int month) {
/**
* @description:迭代算法求解斐波那契數列
* @param month
* @createDate: 2020/7/4 21:44
* @return: int
*/
int a = 2, b = 2, c, count = 1;
while (count < month) {
c = a;
a = a + b;
b = c;
count++;
}
return a;
}
//考慮使用遞歸完成計算
public static int Fibonacci(int month) {
/**
* @description:運用遞歸思想解決斐波那契數列
* @param month
* @createDate: 2020/7/4 21:46
* @return: int
*/
if (month == 1 || month == 2) {
return 2;
} else {
return Fibonacci(month - 1) + Fibonacci(month - 2);
}
}
}
迭代求解思路
程序2:素數問題
題目概述:
判斷101-200之間有多少素數,並輸出所有素數。
素數又稱爲質數,一個大於1的自然數,除了1和它自身之外,不能被其他任何自然數整除的數叫素數。
**判斷素數的方法:**用一個數分別去除2到sqrt(這個數),如果能被整除,則表示這個數不是素數,反之是素數。
注: 之所以除到這個數的算數平方根,是因爲任何一個數,都只能分解一個小於另一個大於其算數平方根的因子(或鏈兩個因子相同,即是該數的算數平方根)
代碼:
public class Programming2 {
public static void main(String[] args) {
int num;
for (int i = 101; i <= 200; i++) {
num = i;
if (isPrime(num)) {
System.out.println(i + "\t");
}
}
}
public static boolean isPrime(int num) {
/**
* @description:判斷整數num是否爲素數
* @param num
* @createDate: 2020/7/4 22:06
* @return: boolean
*/
boolean res = true;
for (int i = 2; i < Math.sqrt(num); i++) {
if (num == 2) {
res = true;
} else {
if (num % i == 0) {
res = false;
break;
}
}
}
return res;
}
}
程序3:水仙花數
題目概述:
打印所有的水仙花數
水仙花數:是指一個三位數,其各位數字立方和等於該數字本身。例如:153是一個“水仙花數”,因爲153=1的三次方+5的三次方+3的三次方。
代碼:
public class Programming3 {
public static void main(String[] args) {
for (int i = 101; i < 1000; i++) {
int num = i;
if (isRes(num)) {
System.out.print(i + "\t");
}
}
}
public static boolean isRes(int number) {
/**
* @description:判斷一個數是否爲水仙花數
* @param number
* @createDate: 2020/7/4 22:12
* @return: boolean
*/
boolean res = false;
int a = number / 100;//取數字的百位
int b = number % 100 / 10;//取數字的十位
int c = number % 10;//取數字的各位
if (number == (a * a * a + b * b * b + c * c * c)) {
res = true;
}
return res;
}
}
程序4:正整數分解質因數
題目概述:
將一個正整數分解質因數。例如:輸入90,打印出90=233*5 。
程序分析:對n進行分解質因數,應先找到一個最小的質數k,,然後按下述步驟完成:
(1)如果這個質數恰等於n,則說明分解質因數的過程已經結束,打印出即可。
(2)如果n > k,但n能被k整除,則應打印出k的值,並用n除以k的商,作爲新的正整數n,重複執行第一步。
(3)如果n不能被k整除,則用k+1作爲k的值,重複執行第一步。
代碼:
public class Programming4 {
public static void main(String[] args) {
System.out.println("請輸入一個整數:");
Scanner scanner = new Scanner(System.in);
float n = scanner.nextFloat();
System.out.print((int) (n) + " = ");
for (int k = 2; k <= n; ) {
if (n == k) {
System.out.println(k);
break;
} else if (n % k == 0) {
n = n / k;
System.out.print(k + "*");
} else {
k++;
}
}
}
}
程序5:成績等級判定
題目概述:
敲寫一個用來判斷成績的代碼,其中90以上爲‘A’,80-90之間爲‘B’,60-80之間爲‘C’,60分以下爲‘D’
代碼:
public class Programming5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(function(101));
System.out.println(function(91));
}
public static String function(double score) {
String res = "您輸入的成績有誤。";
if (score >= 90 & score <= 100) {
res = "A";
} else if (score >= 80 & score < 90) {
res = "B";
} else if (score >= 60 & score < 80) {
res = "C";
} else if (score < 60 & score >= 0) {
res = "D";
}
return res;
}
}
程序6:求兩個數的最大公約數和最小公倍數
題目概述:
輸入兩個正整數 m, n ,求最大公約數和最小公倍數
輾轉相除法:
用較小數除較大數,再用出現的餘數(第一餘數)去除除數,再用出現的餘數(第二餘數)去除第一餘數,如此反覆,直到最後餘數是0爲止。如果是求兩個數的最大公約數,那麼最後的除數就是這兩個數的最大公約數。
代碼:
public class Programming6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int res1 = f(num1, num2);
int res2 = num2 * num1 / res1;
System.out.println("最大公約數:" + res1 + "\t最小公倍數:" + res2);
System.out.println("最大公約數:" + gcd(num1, num2) + "\t最小公倍數:" + res2);
}
public static int f(int x, int y) {
/**
* @description:輾轉相除法求取最大公約數迭代求解
* @param x
* @param y
* @createDate: 2020/7/4 23:10
* @return: int
*/
int t;
if (x < y) {
t = x;
x = y;
y = t;
}
while (y != 0) {
if (x == y) {
return x;
} else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
public static int gcd(int x, int y) {
/**
* @description:輾轉相除法求取最大公約數的遞歸求解
* @param x
* @param y
* @createDate: 2020/7/4 23:11
* @return: int
*/
int t;
if (x < y) {
t = x;
x = y;
y = t;
}
if (x == y || y == 0) {
return x;
} else {
return gcd(y, x % y);
}
}
}
程序7:檢索字符串字母數字空格
題目概述:
輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數
代碼:
public class Programming7 {
public static void main(String[] args) {
int digital = 0;
int letter = 0;
int other = 0;
int blank = 0;
char[] ch = null;
System.out.println("請輸入目標字符串:");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] >= '0' && ch[i] <= '9') {
digital++;
} else if ((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) {
letter++;
} else if (ch[i] == ' ') {
blank++;
} else {
other++;
}
}
System.out.println("數字個數爲:" + digital);
System.out.println("字母個數爲:" + letter);
System.out.println("空格個數爲:" + blank);
System.out.println("其它字符個數爲:" + other);
}
}
程序8:求sum=a+aa+aaa+aaaa+aa…a的值
題目概述:
代碼:
public class Programming8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入數字的值:");
int base = scanner.nextInt();
System.out.println("請輸入執行的次數:");
int n = scanner.nextInt();
//循環累加
int a = base, sum = base;
for (int i = 1; i < n; i++) {
a = a * 10 + 2;
sum = sum + a;
}
System.out.println("結果爲:"+ sum);
}
}
程序9:完數
題目概述:
求1000以內的完數
完數:一個數恰好等於它的因子之和
代碼:
public class Programming9 {
public static void main(String[] args) {
for (int j = 1; j <= 1000; j++) {
int sum = 0;
for (int i = 1; i < j; i++) {
if (j % i == 0) {
sum = sum + i;
}
}
if (sum == j) {
System.out.println(j + " ");
}
}
}
}
程序10:彈性球反彈問題
題目概述:
一個球從100米高度自由落下,每次落地後反跳回原高度的一半再落下,求它在10次落地時,共經過多少米,第十次反彈有多高。
代碼:
public class Programming10 {
public static void main(String[] args) {
double h = 100;
double s = h;
for (int i = 1; i < 10; i++) {
s = s + h;
h = h / 2;
}
double h10 = h / 2;
System.out.println("10次落地時,共經過" + s + "米\t" + "第十次反彈高度爲:" + h10 + "米");
}
}