close
題目概要:
寫出一個字典的程式。
解題方向:
1. 使用split的方法把查詢字跟翻譯過後的字分開。
2. 並使用map存放。 Ps 如用一般陣列做存放的話在Uva OnlineJudge可能執行時間會超過。
3. 讀入要查詢的字,並輸出翻譯後的字。
程式碼:
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
//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"); | |
} | |
} | |
} |
文章標籤
全站熱搜