Java程序設計(2021春)——第四章接口與多態課後題(選擇題+編程題)答案與詳解

Java程序設計(2021春)——第四章接口與多態課後題(選擇題+編程題)答案與詳解


第四章選擇題

Tip:選擇題部分我只針對部分我出錯的或我認為有價值的題目在此整理。

4.0 導學


4.1接口


4.2.1-4.2.2-類型轉換


4.2.3-方法的查找

T2

題面

已知

import java.io.*;

class Person {
	public static void print() {
		System.out.print("Person");
	}
}

class Employee extends Person {
	public void print() {
		System.out.print("Employee");
	}
}

class Manager extends Employee {
	public void print() {
		System.out.print("Manager");
	}
}

public class Test {
	public static void main(String[] args) {
		Manager man = new Manager();
		Employee emp1 = new Employee();
		Employee emp2 = (Employee) man;
		Person person = (Person) man;
		emp2.print();
		System.out.print("#");
		person.print();
	}

}

對於以上代碼,其輸出結果是

A Employee#Person

B Manager#Person

C Employee#Manager

D Manager#Manager

答案

D

詳解

man所指向的對象是Manager()類型的,emp2為經由強轉的man的引用,



3.5 泛型

T1

題面

Java泛型機制的優點有

A 可以使代碼編寫變得簡單

B 比較安全

C 消除對Object類的強制類型轉換=

D 使得代碼運行性能增強

答案

A B C

詳解


第四章編程題

T1 字符串數字驗證

題面

驗證一個給定的字符串是否為數字。是則輸出1,不是則輸出0 一些例子:

"0"=> true

" 0.1 "=> true

"abc"=> false

"1 a"=> false

"2e10"=>true

樣例輸入:

2e10

樣例輸出:

1

思考和詳解

本題就是一個模擬題,只要知道什麼樣的字符串符合題目的要求,直接模擬即可,不過筆者思考了一會,並沒有把所有的情況確定出來,以下這份代碼只拿到了\(70\)分,若有讀者知道筆者疏漏在哪歡迎指出。

具體代碼

import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String str;
		boolean isDot = false;
		boolean isE = false;
		boolean isC = false;
		boolean prt = false;
		str = in.next();
		int len = str.length();
		for (int i = 0; i < len; i++) {
			char c = str.charAt(i);
			if (c >= '0' && c <= '9') {
				isC = true;
				continue;
			} else if (c == '.') {
				if (isDot == true || isE == true) {
					System.out.println(0);
					prt = true;
					break;
				} else {
					isDot = true;
				}
			} else if (c == 'e') {
				if (isE == true || i == 0) {
					System.out.println(0);
					prt = true;
					break;
				} else {
					isE = true;
				}
			} else if (c == 'f' && i == len - 1) {
				continue;
			} else {
				System.out.println(0);
				prt = true;
				break;
			}
		}
		if (!prt) {
			System.out.println(1);
		}
	}
}

T2 數組跳躍

題面

給定一個長度為N的非負的整數數組a[N],初始位置是數組的第一個位置,數組中的每一個數代表你至多能跳多遠,如a[i]=3代表可以從a[i]調到a[i+3],判斷你能否成功跳到數組的最後一個位置。輸入為兩行,第一行為數組的長度N,第二行為N個數,輸出為0表示失敗,1表示成功。

樣例輸入:

5
2 3 1 1 4

樣例輸出:

1

樣例輸入2:

5

3 2 1 0 4

輸出:

0

樣例解釋

如輸出所示

思考和詳解

本題需要注意a[i]代指的時可以至多跳到a[i+a[i]]的位置,而非只能。主幹思路就是一個普通的帶記憶化的\(BFS\)

具體代碼

import java.util.Scanner;

public class arr {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] arr = new int[n + 5];
		int[] flag = new int[n + 105];
		for (int i = 0; i < n; i++) {
			arr[i] = in.nextInt();
			flag[i] = 0;
		}
		int[] queue = new int[n + 105];
		int rear = -1;
		int head = 0;
		queue[++rear] = 0;
		flag[0] = 1;
		while (rear >= head) {
			int p = queue[head++];
			if (p == n - 1) {
				System.out.println(1);
				System.exit(0);
			}
			for (int i = 1; i <= arr[p]; i++) {
				if (p + i < n && flag[p + i] == 0) {
					queue[++rear] = p + i;
					flag[p + i] = 1;
				}
			}
		}
		System.out.println(0);
	}
}

T3跳躍數組2

題面

給定一個長度為N的非負的整數數組a[N],初始位置是數組的第一個位置,數組中的每一個數代表你至多能跳多遠,如a[i]=3代表可以從a[i]調到a[i+3],你的目標是以最小的跳躍次數到達終點。輸入為兩行,第一行為數組的長度N,第二行為N個數,輸出為跳躍次數。

樣例輸入

5
2 3 1 1 4

樣例輸出

2 

樣例解釋

\(2->3->4\),兩次跳躍

思考與詳解

本題和\(T2\)很像,但是有以下兩點不同:

  1. 要求輸出最小的跳躍次數,並且沒有說明如果不能跳到終點怎麼辦,即,暗示所有給定輸入都有解。
  2. 要求輸出最小的跳躍次數。

思路和T2差不多,只需要將flag數組存儲內容變成到該位置的最小部屬即可,感覺有點像dp,但是筆者算法比較菜就不賣弄了。

代碼如下:

import java.util.Scanner;

public class squareNumber {
	public static int n, ct = 0;
	public static int[] arr;
	public static int ans;

	public static void findSquareNumber() {
		for (int i = 1; i <= n; i++) {
			if (i * i <= n) {
				arr[ct++] = i * i;
			}
		}
	}

	public static void func(int nowNum, int cnt, int flag) {
		if (nowNum > n)
			return;
		else if (nowNum == n) {
			ans = ans < cnt ? ans : cnt;
			return;
		} else {
			for (int i = ct - 1; i >= flag; i--) {
				func(nowNum + arr[i], cnt + 1, i);
			}
		}
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		in.close();
		arr = new int[n + 100];
		findSquareNumber();
		ans = n + 100;
		func(0, 0, 0);

		System.out.println(ans);
	}
}

百度了一下發現和leetcode 279是一樣的題目,考察的知識點是動態規劃

這份官方題解給出了不錯的思路,我就不再贅述,直接在下面給出本題的完整代碼。

具體代碼

import java.util.Scanner;

public class arr2 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] arr = new int[n + 5];
		int[] flag = new int[n + 105];
		for (int i = 0; i < n; i++) {
			arr[i] = in.nextInt();
			flag[i] = 0;
		}
		int[] queue = new int[n + 105];
		int rear = -1;
		int head = 0;
		queue[++rear] = 0;
		flag[0] = 0;
		while (rear >= head) {
			int p = queue[head++];
			if (p == n - 1) {
				System.out.println(flag[p]);
				System.exit(0);
			}
			for (int i = 1; i <= arr[p]; i++) {
				if (p + i < n && flag[p + i] == 0) {
					queue[++rear] = p + i;
					flag[p + i] = flag[p] + 1;
				}
			}
		}
		System.out.println(0);
	}
}