7-8 人工智能打招呼
號稱具有人工智能的機器人,至少應該能分辨出新人和老朋友,所以打招呼的時候應該能有所區別。本題就請你為這個人工智能機器人實現這個功能:當它遇到陌生人的時候,會說:“Hello X, how are you?”其中 X 是這個人的稱呼;而當它再次遇到這個人的時候,會說:“Hi X! Glad to see you again!”
輸入格式:
輸入首先在第一行中給出一個正整數 N(≤105),隨后一行給出 N 個人的編號。即簡單起見,我們把每個人的稱呼 X 用一個 5 位整數來替代。
輸出格式:
對于每個人的編號,按照題面要求在一行中輸出人工智能機器人打招呼的內容。
輸入樣例:
7
00000 99999 00000 12345 00000 12345 00000
輸出樣例:
Hello 00000, how are you?
Hello 99999, how are you?
Hi 00000! Glad to see you again!
Hello 12345, how are you?
Hi 00000! Glad to see you again!
Hi 12345! Glad to see you again!
Hi 00000! Glad to see you again!
Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class Main {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(in.readLine());String[] input = in.readLine().split(" ");int[] book = new int[1000000];StringBuilder sb = new StringBuilder();for (int i = 0; i < n; i++) {int x = Integer.parseInt(input[i]);if (book[x] != 1) {sb.append("Hello ").append(input[i]).append(", how are you?");book[x] = 1;} else {sb.append("Hi ").append(input[i]).append("! Glad to see you again!");}if (i < n - 1) sb.append("\n");}System.out.println(sb);}
}
7-9 人工智能數字翻譯
千萬不要以為地球上的人類都用十進制數字。事實上,地球上不同國家、不同民族在用的數字系統多達十幾套。例如新幾內亞的 Oksapmin 人用的是 27 進制的數字系統,如上圖所示,他們用身體的 27 個部位表示 27 個數字。例如他們管 1 叫“提普納”,即一只手的大拇指;管 6 叫“都怕”,是指手腕;管 12 叫“納塔”,指耳朵,等等。于是他們那里的 10 是十進制系統里的 27,20 是十進制里的 54 …… 當兩個不同數字系統的人類碰到一起談價錢,大家就很頭痛了,于是搞了一個人工智能數字翻譯,可以把數字從一個系統翻譯到另一個系統。本題就請你來實現這個人工智能數字翻譯功能。
輸入格式:
輸入在第一行中給出兩個非負整數:A(≤108 —— 這里 108 是十進制數)和 D,其中 A 是 10 或 27 進制數,D 是 A 的進制,或者是 10,或者是 27。如果 A 是 27 進制數,我們用 0 - 9 表示前 10 個數,用 A - Q 表示后面 17 個數。
輸出格式:
在一行中將輸入的數字翻譯成另一個進制系統的數字。
輸入樣例 1:
6397636 10
輸出樣例 1:
C10OD
輸入樣例 2:
8E0A 27
輸出樣例 2:
167680
Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class Main {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String[] input = in.readLine().split(" ");int D = Integer.parseInt(input[1]);if (D == 10) {System.out.println(Integer.toString(Integer.parseInt(input[0]), 27).toUpperCase());} else {System.out.println(Integer.parseInt(input[0], 27));}}
}
7-10 機器人拼圖
給定一塊由 n×m 個格子組成的矩形拼圖板,本題要求你根據給定的機械手移動指令集,將拼圖中的碎片逐一放到指定位置。
機械手每次抓取一塊碎片,都會在拼圖板的左上角位置等待指令。一個指令集是由 0-4 這五個數字組成的字符串,每個數字代表的意義如下:
- 1:向右移動一格;
- 2:向下移動一格;
- 3:向左移動一格;
- 4:向上移動一格;
- 0:將碎片放置在當前位置,并結束這次任務。
如果指令要求機械手移動到拼圖板邊界外,機械手會無視這個指令。如果接收到指令 0 時,當前位置上已經有一塊碎片放好了,機械手會扔掉手里的碎片,結束這次任務。
輸入格式:
輸入第一行給出 2 個正整數 n 和 m(1≤n,m≤100),隨后一共有 n×m 行,第 i 行給出編號為 i(i=1,?nm)的碎片對應的指令集,每條指令集一定以唯一的 0 結尾。
輸出格式:
輸出 n 行,每行 m 個整數,為放置在對應位置上的碎片編號。如果該位置上沒有碎片,則輸出 0。一行中的數字間以 1 個空格分隔,行首位不得有多余空格。
輸入樣例:
2 3
1120
21140
34120
0
110
21111340
輸出樣例:
4 6 2
0 3 1
Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class Main {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String[] input = in.readLine().split(" ");int n = Integer.parseInt(input[0]);int m = Integer.parseInt(input[1]);int row = n * m;int[][] map = new int[n + 1][m + 1];for (int i = 1; i <= row; i++) {char[] c = in.readLine().toCharArray();int x = 1, y = 1;for (int j = 0; j < c.length; j++) {int op = c[j] - '0';if (op == 1 && y != m) {y++;} else if (op == 2 && x != n) {x++;} else if (op == 3 && y != 1) {y--;} else if (op == 4 && x != 1) {x--;}}if (map[x][y] == 0) {map[x][y] = i;}}StringBuilder sb = new StringBuilder();for (int i = 1; i < n + 1; i++) {for (int j = 1; j < m + 1; j++) {sb.append(map[i][j]);if (j < m) sb.append(" ");}if (i < n) sb.append("\n");}System.out.println(sb);}
}
7-11 PAT 基礎級排名
PAT 的段位分為 9 個級別,其中基礎級考試分為青銅、白銀、黃金、白金、鉆石這 5 個段位,滿分分別為 30、50、60、80、100。
基礎級考試由報考單位訂制需要的最高級別,設為級別 L,根據考生獲得的分數所在的分數段決定該生獲得的證書級別和證書內容,規則為:
- 若考生的分數超過段位 L1 的滿分,且不高于段位 L2 的滿分,并且 L1 和 L2 為相鄰段位,則該生獲得 L2 段位的證書;
- 證書上包含兩個成績:S/F 和 R/T,其中 S 是本人得分;F 是該段位滿分;R 是本人在該段位證書獲得者中的排名;T 是位于該段位及以下段位考生總人數。
給定一組考生的成績,請你編寫程序生成他們的證書。
輸入格式:
輸入首先在第一行給出 2 個正整數:一個是不超過 1000 的正整數 N,為考生總人數;另一個是單位訂制的最高段位級別 L,在 [1, 5] 區間內,依次對應青銅、白銀、黃金、白金、鉆石這 5 個段位。
隨后 N 行,每行給出一位考生的準考證號和成績,其間以空格分隔。準考證號為長度不超過 10 的、由數字和英文字母組成的字符串;成績為不超過段位 L 的滿分的非負整數。
輸出格式:
按照成績從高到低的順序輸出每位考生的證書信息,格式為:
準考證號 段位 S/F R/T
成績并列的考生擁有并列的排名,其后的考生名次按實際位次計算。例如有 5 位考生得到滿分 100 時,他們都是第 1 名,而考 99 的考生就是第 6 名。成績并列的考生按準考證號的字典序遞增輸出。題目保證準考證號沒有重復。
注意:零分沒有證書。對于零分的考生,只按字典序遞增輸出其準考證號。
輸入樣例:
10 4
CN001 58
CN012 49
CN233 0
CN003 0
CN025 31
CN999 80
CN666 80
CN777 60
CN007 79
CN250 15
輸出樣例:
CN666 4 80/80 1/10
CN999 4 80/80 1/10
CN007 4 79/80 3/10
CN777 3 60/60 1/7
CN001 3 58/60 2/7
CN012 2 49/50 1/5
CN025 2 31/50 2/5
CN250 1 15/30 1/3
CN003
CN233
Solution:
::: danger
測試點 1、4 答案錯誤
:::
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;public class Main {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String[] input = in.readLine().split(" ");int n = Integer.parseInt(input[0]);int l = Integer.parseInt(input[1]);List<Rank> list = new ArrayList<>();for (int i = 0; i < n; i++) {input = in.readLine().split(" ");int score = Integer.parseInt(input[1]);int L = 0;if (score <= 30) list.add(new Rank(input[0], 1, score, 30));else if (score <= 50) list.add(new Rank(input[0], 2, score, 50));else if (score <= 60) list.add(new Rank(input[0], 3, score, 60));else if (score <= 80) list.add(new Rank(input[0], 4, score, 80));else if (score <= 100) list.add(new Rank(input[0], 5, score, 100));}list.sort(null);int index = 1, cnt = 0, t = 0;Rank temp = list.get(0);StringBuilder sb = new StringBuilder();int size = list.size();for (int i = 0; i < size; i++) {Rank rank = list.get(i);if (rank.score == 0) {sb.append(rank.num);} else {sb.append(rank.num).append(" ").append(rank.L).append(" ").append(rank.score).append("/").append(rank.fullScore).append(" ");if (temp.score == rank.score && temp.L == rank.L) {sb.append(index).append("/").append(n);t++;} else if (temp.score != rank.score && temp.L == rank.L) {index = t + 1;sb.append(index).append("/").append(n);t++;} else {temp = list.get(i);index = 1;n -= t;sb.append(index).append("/").append(n);t = 1;}}cnt++;if (i < size - 1) sb.append("\n");}System.out.println(sb);}
}class Rank implements Comparable<Rank> {String num;int L;int score;int fullScore;public Rank(String num, int l, int score, int fullScore) {this.num = num;L = l;this.score = score;this.fullScore = fullScore;}@Overridepublic int compareTo(Rank o) {int r = o.score - this.score;if (r == 0) r = this.num.compareTo(o.num);return r;}
}
7-12 人工智能刑警
人工智能技術在刑偵領域已經有了長足的發展,可以在擁擠的人群中識別出每個人的面部特征,與數據庫中所有罪犯的面部特征進行匹配,自動識別出其中的罪犯。
本題就請你實現一個簡單的自動識別程序,根據系統抽取出的若干面部特征判斷其是否罪犯。
輸入格式:
輸入第一行給出兩個正整數 N(≤2×104)和 M(≤10),分別為數據庫中罪犯的數量和每個人臉提取的不同特征的數量。隨后 N 行,每行給出 M 個整數,以空格分隔。第 i 行給出的是第 i 號罪犯的面部特征(1≤i≤N),格式為:
特征1 特征2 …… 特征M ID
其中每個特征
是一個 [1,109] 區間內的整數;ID
是罪犯的編號,為不超過 20 位的正整數。注意不同位置的特征對應不同種類,例如特征1
對應眼距、特征2
對應唇寬等等。題目保證不存在任何兩個罪犯具有完全相同的面部特征,所有 ID
也都是不同的。
再下面給出一系列捕捉到的人臉的面部特征。當特征1
為 0
時標志輸入結束,這一行輸入不要處理。此外,為了幫助你區分罪犯和普通人的數據,這兩部分數據之間有一個空行。(因為是簡單題,所以查詢數據量比較小,不超過 2000。當然你可以考慮一下,如果查詢數量巨大應該怎么處理……)
輸出格式:
對每個捕捉到的人臉,如果與某個罪犯完全匹配,則在一行中輸出罪犯編號(注意:如果有前導零也必須原樣輸出);如果沒有匹配任何罪犯,則輸出 Pass
。
輸入樣例:
9 4
15 23 23 7 0001
12 32 15 5 2001
10 15 23 3 3725
12 17 15 6 0238
15 10 23 11 99674
29 32 33 4 00253
9 31 987 13 1004
29 32 15 12 8791
15 23 25 7 772015 23 23 7
12 17 15 6
10 15 15 3
29 32 33 4
15 32 12 29
9 31 987 13
5 10 23 1
0 0 0 0
輸出樣例:
0001
0238
Pass
00253
Pass
1004
Pass
Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;public class Main {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String[] input = in.readLine().split(" ");int n = Integer.parseInt(input[0]);int m = Integer.parseInt(input[1]);Map<String, String> map = new HashMap<>();for (int i = 0; i < n; i++) {input = in.readLine().split(" ");StringBuilder sb = new StringBuilder();for (int j = 0; j < m; j++) {sb.append(input[j]);if (j < m - 1) sb.append(" ");}map.put(sb.toString(), input[m]);}in.readLine();input = in.readLine().split(" ");while (!input[0].equals("0")) {StringBuilder sb = new StringBuilder();for (int j = 0; j < m; j++) {sb.append(input[j]);if (j < m - 1) sb.append(" ");}System.out.println(map.getOrDefault(sb.toString(), "Pass"));input = in.readLine().split(" ");}}
}