本文整理 5 道經典 Java 基礎階段測試題,涵蓋二維數組操作、數學建模、遞歸與迭代、位運算優化、贪心算法等核心考點,提供完整代碼實現與多種解法對比。
鍵盤輸入行數 n,打印前 n 行楊輝三角,每行居中顯示。
arr[i][j] = arr[i-1][j] + arr[i-1][j-1]public class YangHuiTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入行數: ");
int rows = sc.nextInt();
sc.close();
int[][] triangle = new int[rows][];
// 1. 初始化每行長度、首尾賦值 1
for (int i = 0; i < rows; i++) {
triangle[i] = new int[i + 1];
triangle[i][0] = triangle[i][i] = 1;
}
// 2. 填充中間值
for (int i = 2; i < rows; i++) {
for (int j = 1; j < i; j++) {
triangle[i][j] = triangle[i-1][j] + triangle[i-1][j-1];
}
}
// 3. 打印 (居中縮進)
printTriangle(triangle);
}
static void printTriangle(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
// 縮進: 每行前打印 (總行數 - 當前行 - 1) 個制表符
for (int k = 0; k < arr.length - i - 1; k++) System.out.print("\t");
// 打印數值
for (int val : arr[i]) System.out.print(val + "\t");
System.out.println();
}
}
}
小球從 100m 高度落下,每次彈起高度為原高度一半。求第 10 次落地時總路程、第 10 次彈起高度。
public class BouncingBall {
public static void main(String[] args) {
double height = 100.0;
double distance = height; // 第一次落下
for (int i = 1; i <= 10; i++) {
height /= 2; // 彈起高度
if (i < 10) distance += height * 2; // 彈起+落下
if (i == 10) System.out.printf("第10次彈起高度: %.4f 米%n", height);
}
System.out.printf("第10次落地總路程: %.4f 米%n", distance);
}
}
輸出:第 10 次彈起高度 ≈ 0.0977 米,總路程 ≈ 299.6094 米。
一對兔子第 3 個月開始每月產一對,新生兔第 3 月同樣產仔,不死。求第 20 月總數。
經典斐波那契數列:F(1)=1, F(2)=1, F(n)=F(n-1)+F(n-2) (n≥3)
第 n 月兔子對數 = F(n),總隻數 = 2 × F(n)
| 方法 | 時間複雜度 | 空間複雜度 | 代碼關鍵點 |
|---|---|---|---|
| 遞歸 | O(2^n) 指數級 | O(n) 棧 | 簡潔但極慢,n=40 明顯卡頓 |
| 數組迭代 | O(n) | O(n) | 自底向上,存儲歷史值 |
| 滾動變量 | O(n) | O(1) ⭐ | 僅保留前兩值,最優 |
public class RabbitFibonacci {
public static void main(String[] args) {
int month = 20;
long a = 1, b = 1; // F(1), F(2)
for (int i = 3; i <= month; i++) {
long c = a + b;
a = b;
b = c;
}
System.out.printf("第 %d 月兔子總數: %d 隻%n", month, b * 2);
}
}
結果:第 20 月共 13462 隻。
n 盞燈初始全關。第 i 個學生拉動 i 的倍數編號燈的開關。n 位學生過後,亮燈數量?
public class LightSwitch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("輸入燈數/學生數 n (1-65535): ");
int n = sc.nextInt();
sc.close();
if (n < 1 || n > 65535) { System.out.println("輸入超出範圍"); return; }
// 數學解: 完全平方數個數 = floor(sqrt(n))
int count = (int) Math.sqrt(n);
System.out.println("亮燈數量: " + count);
// 驗證: 打印亮燈編號
for (int i = 1; i * i <= n; i++) {
System.out.print(i * i + " ");
}
}
}
模擬解優化技巧:內層循環 for (int j = i; j <= n; j += i) 直接遍歷倍數,避免取模判斷。
啤酒 2 元/瓶,2 空瓶或 4 蓋子換 1 瓶。10 元最多喝幾瓶?(不可賒欠)
新瓶數 = 空瓶/2 + 蓋子/4空瓶 = 空瓶%2 + 新瓶數、蓋子 = 蓋子%4 + 新瓶數public class BeerExchange {
static int total = 0;
public static void main(String[] args) {
int initial = 10 / 2; // 5 瓶
total = initial;
exchange(initial, initial); // 初始空瓶=蓋子=5
System.out.println("剩余空瓶: " + bottles);
System.out.println("剩余蓋子: " + caps);
System.out.println("最多可喝: " + total + " 瓶");
}
static int bottles, caps;
static void exchange(int bottle, int cap) {
if (bottle >= 2 || cap >= 4) {
int newBottles = bottle / 2 + cap / 4;
total += newBottles;
bottles = bottle % 2 + newBottles;
caps = cap % 4 + newBottles;
exchange(bottles, caps);
}
}
}
結果:最多喝 9 瓶,剩 1 空瓶、1 蓋子。
迭代版本:用 while 循環替代遞歸,避免棧溢出風險(雖本題數據量極小)。