|
|
back to boardWhat's am I wrong? Posted by Axeus 4 Apr 2011 13:42 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; public class Phone { public static boolean zero(char input, char output) { return input == '0' && (output == 'o' || output == 'q' || output == 'z'); } public static boolean one(char input, char output) { return input == '1' && (output == 'i' || output == 'j'); } public static boolean two(char input, char output) { return input == '2' && (output == 'a' || output == 'b' || output == 'c'); } public static boolean three(char input, char output) { return input == '3' && (output == 'd' || output == 'e' || output == 'f'); } public static boolean four(char input, char output) { return input == '4' && (output == 'g' || output == 'h'); } public static boolean five(char input, char output) { return input == '5' && (output == 'k' || output == 'l'); } public static boolean six(char input, char output) { return input == '6' && (output == 'm' || output == 'n'); } public static boolean seven(char input, char output) { return input == '7' && (output == 'p' || output == 'r' || output == 's'); } public static boolean eight(char input, char output) { return input == '8' && (output == 't' || output == 'u' || output == 'z'); } public static boolean nine(char input, char output) { return input == '9' && (output == 'w' || output == 'x' || output == 'y'); } public static void main(String[] args) throws IOException { BufferedReader bin = new BufferedReader( new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( System.out)); String result = ""; ArrayList<String> dataStore = new ArrayList<String>(); String data = ""; boolean checked = false; while (!data.equals("-1")) { data = bin.readLine(); dataStore.add(data); }
int i =0; String callNum =dataStore.get(i++); int callNumindex = 0; while(!callNum.equals("-1")){ int letterLine = Integer.parseInt(dataStore.get(i++)); for (int j = i; j < letterLine+i; j++) { String letter = dataStore.get(j); for (int k = 0; k < letter.length(); k++) { char input = callNum.charAt(callNumindex); char output = letter.charAt(k); checked = zero(input, output)||one(input, output)||two(input, output)|| three(input, output)||four(input, output)||five(input, output)|| six(input, output)||seven(input, output)||eight(input, output)|| nine(input, output); if(checked){ result += output; callNumindex++; if(k == letter.length()-1) result+=" "; continue; }else k=letter.length(); }
} if(result.length()-2==callNum.length()){ result = result.substring(0,result.length()-1); System.out.println(result); //bw.write(result+'\n'); }else System.out.println("No solution."); //bw.write("No solution.\n"); i += letterLine; callNum =dataStore.get(i++); callNumindex = 0; result =""; } bw.close(); } } ***** I test on my laptop, the output is the same as example. But the answer i got is wrong answer on test 1. So confusing. |
|
|