close

題目概要:

寫出一個字典的程式。

解題方向:

1. 使用split的方法把查詢字跟翻譯過後的字分開。

2. 並使用map存放。 Ps 如用一般陣列做存放的話在Uva OnlineJudge可能執行時間會超過。

3. 讀入要查詢的字,並輸出翻譯後的字。

程式碼:

//Java
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
class uva10282{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
Map<String,String> map=new HashMap<String,String>();
while(true){
String st=sc.nextLine();
if(st.equals(""))break;
String []temp=st.split(" "); //切割字串,把查詢字根翻譯後的字分開。
map.put(temp[1],temp[0]); //存入map。
}
while(sc.hasNext()){
String st=sc.next();
//Output。
if(map.get(st)!=null) System.out.println(map.get(st));
else System.out.println("eh");
}
}
}
view raw uva10282.java hosted with ❤ by GitHub

arrow
arrow
    文章標籤
    Java
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(0) 人氣()