So this problem wont work on uva however I am pretty sure that my implementation is correct. Really simple problem.
import java.io.PrintWriter;
import java.util.Scanner;
/**
*
* @author Sanchit M. Bhatnagar
* @see http://uhunt.felix-halim.net/id/74004
*
*/
public class P11956 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int T = Integer.parseInt(sc.nextLine());
for (int zz = 1; zz <= T; zz++) {
int idx = 0;
int[] byteArray = new int[100];
char[] input = sc.nextLine().trim().toCharArray();
for (int i = 0; i < input.length; i++) { switch (input[i]) { case '>‘:
idx++;
break;
case ‘<':
idx--;
break;
case '+':
byteArray[idx]++;
break;
case '-':
byteArray[idx]--;
break;
}
idx = (idx + byteArray.length) % byteArray.length;
byteArray[idx] = (byteArray[idx] + 256) % 256;
}
out.print("Case " + zz + ":");
for (int i = 0; i < byteArray.length; i++) {
String print = String.format("%02x", byteArray[i]).toUpperCase();
out.print(" " + print);
}
out.println();
}
out.close();
sc.close();
}
}
[/java]
It’s working, however it’s wrong idea to use modulo arithmetics. byteArray can be type of bytes, so you don’t have to convert it’s values after every instruction.
Also, idx should be calculated this way: if(idx == 0) idx = 99; else idx–; – it saves about one second in UVa.
Heh, true. I really wanted to use an “unsigned byte” so I could just increment without any worries. Your way works indeed, but my way works if I want to handle the case of -1 and 256 in one line. :)