題目連結
題意:
Soha 都叫 Sparrow 幫他寫作業
題目會告訴你 Sparrow 有哪些擅長科目
以及 Soha 有哪個科目的作業要做
每組測資先給 N 個科目與對應天數
代表 Sparrow 擅長的科目,以及分別完成時間
然後是作業科目(每組只有一個)與期限天數 D
如果作業是 Sparrow 擅長的
也能 D 天內完成,輸出 Yesss
如果擅長且能在 D+5 天內,輸出 Late
如果超過 D+5,或不是擅長的
輸出 Do your own homework!
解法:
先用 Structure, 陣列或 Map 等等
能同時存 2 種型別的資料結構
紀錄擅長科目與天數
接著搜尋作業是否存在資料內
有則抓出天數,與 D 比較
最後輸出對應結果
以下以 HashMap 為例
程式(Java):
題意:
Soha 都叫 Sparrow 幫他寫作業
題目會告訴你 Sparrow 有哪些擅長科目
以及 Soha 有哪個科目的作業要做
每組測資先給 N 個科目與對應天數
代表 Sparrow 擅長的科目,以及分別完成時間
然後是作業科目(每組只有一個)與期限天數 D
如果作業是 Sparrow 擅長的
也能 D 天內完成,輸出 Yesss
如果擅長且能在 D+5 天內,輸出 Late
如果超過 D+5,或不是擅長的
輸出 Do your own homework!
解法:
先用 Structure, 陣列或 Map 等等
能同時存 2 種型別的資料結構
紀錄擅長科目與天數
接著搜尋作業是否存在資料內
有則抓出天數,與 D 比較
最後輸出對應結果
以下以 HashMap 為例
程式(Java):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String args[]) { | |
Scanner sc = new Scanner(System.in); | |
int T = Integer.valueOf(sc.next()); | |
for (int i = 1; i <= T; i++) { | |
HashMap<String, Integer> com = new HashMap<String, Integer>(); | |
int N = Integer.valueOf(sc.next()); | |
while (N-- > 0) | |
com.put(sc.next(), Integer.valueOf(sc.next())); | |
int D = Integer.valueOf(sc.next()); | |
String work = sc.next(); | |
System.out.print("Case " + i + ": "); | |
if (com.get(work) == null) | |
System.out.println("Do your own homework!"); | |
else if (D >= com.get(work)) | |
System.out.println("Yesss"); | |
else if (D + 5 >= com.get(work)) | |
System.out.println("Late"); | |
else | |
System.out.println("Do your own homework!"); | |
} | |
} | |
} |
留言
張貼留言