package org.goro.uva.m;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Brainfuck {
private static final int MEM_SIZE = 100;
private static final char PTR_INC = '>';
private static final char PTR_DEC = '<';
private static final char VAL_INC = '+';
private static final char VAL_DEC = '-';
public static void main(String[] args) throws IOException {
Reader.init(System.in);
StringBuilder sb = new StringBuilder();
int tests = Reader.nextInt();
int currTest = 0;
while (currTest < tests) {
currTest++;
String instructions = Reader.next();
sb.append("Case " + (currTest) + ":");
processInstructions(instructions, sb);
sb.append("\n");
}
System.out.print(sb);
}
private static void processInstructions(String instructions,
StringBuilder sb) {
byte[] memory = new byte[MEM_SIZE];
int pointer = 0;
for (int i = 0; i < instructions.length(); i++) {
char instruction = instructions.charAt(i);
switch (instruction) {
case PTR_DEC:
pointer = (pointer == 0) ? MEM_SIZE - 1 : pointer - 1;
break;
case PTR_INC:
pointer = (pointer == MEM_SIZE - 1) ? 0 : pointer + 1;
break;
case VAL_INC:
memory[pointer]++;
break;
case VAL_DEC:
memory[pointer]--;
break;
default:
break;
}
}
for (int i = 0; i < memory.length; i++) {
sb.append(" " + convert(memory[i]));
}
}
private static String convert(byte n) {
return String.format("%02X", n);
}
}
/** Class for buffered reading int and double values */
/** http://www.cpe.ku.ac.th/~jim/java-io.html */
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
25/07/2014
11956 - Brainfuck - JAVA
11687 - Digits - JAVA
package org.goro.uva.m;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Digits {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
StringBuilder sb = new StringBuilder();
String line = Reader.next();
while (!line.equals("END")) {
int lenght = line.length();
if (line.equals("1")) {
sb.append(1);
} else if (lenght < 2) {
sb.append(2);
} else if (lenght < 10) {
sb.append(3);
} else {
sb.append(4);
}
line = Reader.next();
if (!line.equals("END")) {
sb.append("\n");
}
}
System.out.println(sb);
}
}
/** Class for buffered reading int and double values */
/** http://www.cpe.ku.ac.th/~jim/java-io.html */
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
11683 - Laser Sculpture - JAVA
package org.goro.uva.m;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class LaserSculpture {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
StringBuilder sb = new StringBuilder();
int height = Reader.nextInt();
while (height > 0) {
int lenght = Reader.nextInt();
int lasers = cut(height, lenght);
sb.append(lasers);
height = Reader.nextInt();
if (height > 0) {
sb.append("\n");
}
}
System.out.println(sb);
}
private static int cut(int maxHeight, int lenght) throws IOException {
int cut = 0;
int last = maxHeight;
for (int j = 0; j < lenght; j++) {
int curr = Reader.nextInt();
if (curr < last) {
cut += last - curr;
}
last = curr;
}
return cut;
}
}
/** Class for buffered reading int and double values */
/** http://www.cpe.ku.ac.th/~jim/java-io.html */
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
Subscribe to:
Posts (Atom)