(UVA) Decoding - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=738&page=show_problem&problem=2536
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTests = Integer.parseInt(br.readLine());
for (int test = 0; test < numTests; test++) {
bw.write("Case " + (test+1) + ": ");
String s = br.readLine();
StringBuilder sb = new StringBuilder();
int index = 0;
while (index < s.length()) {
char caracter = s.charAt(index);
int number = 0;
for(index = index + 1; index < s.length() && Character.isDigit(s.charAt(index)); index++) {
number = number*10 + (s.charAt(index)-'0');
}
for (int i = 0; i < number; i++) {
bw.write(caracter);
}
}
bw.write("\n");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTests = Integer.parseInt(br.readLine());
for (int test = 0; test < numTests; test++) {
bw.write("Case " + (test+1) + ": ");
String s = br.readLine();
StringBuilder sb = new StringBuilder();
int index = 0;
while (index < s.length()) {
char caracter = s.charAt(index);
int number = 0;
for(index = index + 1; index < s.length() && Character.isDigit(s.charAt(index)); index++) {
number = number*10 + (s.charAt(index)-'0');
}
for (int i = 0; i < number; i++) {
bw.write(caracter);
}
}
bw.write("\n");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment