(SPOJ) 2280 - Encontre o telefone - Solução 2
Solução menos eficiente, pois usa o Map.
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
void processa() throws NumberFormatException, IOException {
Map<Character, Character> alfabeto = new TreeMap<Character, Character>();
alfabeto.put('A', '2');
alfabeto.put('B', '2');
alfabeto.put('C', '2');
alfabeto.put('D', '3');
alfabeto.put('E', '3');
alfabeto.put('F', '3');
alfabeto.put('G', '4');
alfabeto.put('H', '4');
alfabeto.put('I', '4');
alfabeto.put('J', '5');
alfabeto.put('K', '5');
alfabeto.put('L', '5');
alfabeto.put('M', '6');
alfabeto.put('N', '6');
alfabeto.put('O', '6');
alfabeto.put('P', '7');
alfabeto.put('Q', '7');
alfabeto.put('R', '7');
alfabeto.put('S', '7');
alfabeto.put('T', '8');
alfabeto.put('U', '8');
alfabeto.put('V', '8');
alfabeto.put('X', '9');
alfabeto.put('Z', '9');
alfabeto.put('W', '9');
alfabeto.put('Y', '9');
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while((line = br.readLine()) != null) {
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == '0' || line.charAt(i) == '1' || line.charAt(i) == '-') {
bw.write(line.charAt(i));
}
else {
bw.write(alfabeto.get(line.charAt(i)));
}
}
bw.write("\n");
}
bw.flush();
bw.close();
return;
}
}
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
void processa() throws NumberFormatException, IOException {
Map<Character, Character> alfabeto = new TreeMap<Character, Character>();
alfabeto.put('A', '2');
alfabeto.put('B', '2');
alfabeto.put('C', '2');
alfabeto.put('D', '3');
alfabeto.put('E', '3');
alfabeto.put('F', '3');
alfabeto.put('G', '4');
alfabeto.put('H', '4');
alfabeto.put('I', '4');
alfabeto.put('J', '5');
alfabeto.put('K', '5');
alfabeto.put('L', '5');
alfabeto.put('M', '6');
alfabeto.put('N', '6');
alfabeto.put('O', '6');
alfabeto.put('P', '7');
alfabeto.put('Q', '7');
alfabeto.put('R', '7');
alfabeto.put('S', '7');
alfabeto.put('T', '8');
alfabeto.put('U', '8');
alfabeto.put('V', '8');
alfabeto.put('X', '9');
alfabeto.put('Z', '9');
alfabeto.put('W', '9');
alfabeto.put('Y', '9');
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while((line = br.readLine()) != null) {
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == '0' || line.charAt(i) == '1' || line.charAt(i) == '-') {
bw.write(line.charAt(i));
}
else {
bw.write(alfabeto.get(line.charAt(i)));
}
}
bw.write("\n");
}
bw.flush();
bw.close();
return;
}
}
Comments
Post a Comment