http://code.google.com/codejam/contest/351101/dashboard#s=p2
A-Z 까지 대응하는 숫자를 미리 다 정의해 놓으면 쉬울 듯 하다.
하지만 이보다 효율적인 코드가 있을텐데………………………...
전에 Perl 카페에서, 본 원라인이 생각나서 적어봅니다.
자바코드 T9_Spelling.java
|
|
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;
public class T9_Spelling {
/** * @param args */ public static void main(String[] args) { String strFileName = "QRA2010_C-large-practice"; String strInputFileName = strFileName + ".in"; String strOutputFileName = strFileName + ".out";
int totalNumber = 0; int outputCaseNumber = 1;
try { //////////////////////////////////////////////////////////////// BufferedReader in = new BufferedReader(new FileReader(strInputFileName)); BufferedWriter out = new BufferedWriter(new FileWriter(strOutputFileName)); String s;
totalNumber = Integer.parseInt(in.readLine());
while ((s = in.readLine()) != null) { System.out.println(s);
char[] charArray = s.toCharArray();
s = "";
for (int i = 0; i < charArray.length; i++) { String strNum = getNumber(charArray[i]);
if (getLastChar(s) == getFirstChar(strNum)) { s = s + " "; }
s = s + strNum; }
s = String.format("Case #%d: %s\n", outputCaseNumber++, s); System.out.print(s); out.write(s); } in.close(); out.close(); //////////////////////////////////////////////////////////////// } catch (IOException e) { System.err.println("ERROR"); System.err.println(e); // 에러가 있다면 메시지 출력 System.exit(1); }
}
private static Object getFirstChar(String strNum) { if (strNum.length() <= 0) { return null; } else { return strNum.charAt(0); } }
private static Object getLastChar(String s) {
if (s.length() <= 0) { return null; } else { return s.charAt(s.length()-1); } }
private static String getNumber(char c) {
String retStr = "";
switch (c) { case 'a': retStr = "2"; break; case 'b': retStr = "22"; break; case 'c': retStr = "222"; break; case 'd': retStr = "3"; break; case 'e': retStr = "33"; break; case 'f': retStr = "333"; break; case 'g': retStr = "4"; break; case 'h': retStr = "44"; break; case 'i': retStr = "444"; break; case 'j': retStr = "5"; break; case 'k': retStr = "55"; break; case 'l': retStr = "555"; break; case 'm': retStr = "6"; break; case 'n': retStr = "66"; break; case 'o': retStr = "666"; break; case 'p': retStr = "7"; break; case 'q': retStr = "77"; break; case 'r': retStr = "777"; break; case 's': retStr = "7777"; break; case 't': retStr = "8"; break; case 'u': retStr = "88"; break; case 'v': retStr = "888"; break; case 'w': retStr = "9"; break; case 'x': retStr = "99"; break; case 'y': retStr = "999"; break; case 'z': retStr = "9999"; break; default: retStr = "0"; break; }
return retStr; }
} |
|
'Programming > Contest' 카테고리의 다른 글
[GCJ] 2012 Qualification Round (0) | 2012.04.15 |
---|---|
GCJ_2012_Registration (0) | 2012.03.14 |
Qualification Round Africa 2010 Problem B. Reverse Words 그래도 예선은 통과해보자. (0) | 2012.03.06 |
코드게이트2012 (0) | 2012.02.24 |
code jam 2012 일정. (0) | 2012.02.23 |
댓글