Commit 2eb9e37b authored by uwdkn's avatar uwdkn

Merge branch 'master' into 'master'

Group 3 exec test cases

See merge request !46
parents 48c9e4a7 167937cb
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
/*
* Reads a brainfuck program (of no more than 1024 instructions) from stdin and executes it.
*
* Examples:
* Read two chars, add their ASCII values, print the result: ,>,<[->+<]>.
* Hello World: ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
*/
class Brainfuck {
public int RIGHT; /* > */
public int LEFT; /* < */
public int INCREMENT; /* + */
public int DECREMENT; /* - */
public int WRITE; /* . */
public int READ; /* , */
public int LOOP_START; /* [ */
public int LOOP_END; /* ] */
public int NEWLINE; /* \n */
public int[] data;
public int dataSize;
public int dataPtr;
public int[] instructions;
public int instructionsSize;
public int instructionPtr;
public void init() {
RIGHT = 62;
LEFT = 60;
INCREMENT = 43;
DECREMENT = 45;
WRITE = 46;
READ = 44;
LOOP_START = 91;
LOOP_END = 93;
NEWLINE = 10;
dataSize = 30000;
data = new int[dataSize];
dataPtr = 0;
instructionsSize = 1024;
instructions = new int[instructionsSize];
}
public int read() throws Exception {
int result = NEWLINE;
while (result == NEWLINE) {
result = System.in.read();
}
return result;
}
public void fuck() throws Exception {
instructionPtr = 0;
while (instructionPtr < instructionsSize && instructions[instructionPtr] != 0) {
int instruction = instructions[instructionPtr];
if (instruction == RIGHT) {
dataPtr = (dataPtr + 1) % dataSize;
} else if(instruction == LEFT) {
dataPtr = (dataPtr - 1) % dataSize;
} else if (instruction == INCREMENT) {
data[dataPtr] = data[dataPtr] + 1;
} else if (instruction == DECREMENT) {
data[dataPtr] = data[dataPtr] - 1;
} else if (instruction == READ) {
data[dataPtr] = read();
} else if (instruction == WRITE) {
System.out.write(data[dataPtr]);
System.out.flush();
} else if (instruction == LOOP_START && data[dataPtr] == 0) {
int index = instructionPtr + 1;
int count = 0;
while (! (instructions[index] == LOOP_END && count == 0)) {
if (instructions[index] == LOOP_START) {
count = count + 1;
} else if (instructions[index] == LOOP_END) {
count = count - 1;
}
index = index + 1;
}
instructionPtr = index;
} else if (instruction == LOOP_END && data[dataPtr] != 0) {
int index = instructionPtr - 1;
int count = 0;
while (! (instructions[index] == LOOP_START && count == 0)) {
if (instructions[index] == LOOP_END) {
count = count + 1;
} else if (instructions[index] == LOOP_START) {
count = count - 1;
}
index = index - 1;
}
instructionPtr = index;
}
instructionPtr = instructionPtr + 1;
}
}
public static void main(String[] args) throws Exception {
Brainfuck fuck = new Brainfuck();
fuck.init();
int index = 0;
int instruction = System.in.read();
while (index < fuck.instructionsSize && instruction != fuck.NEWLINE) {
fuck.instructions[index] = instruction;
instruction = System.in.read();
index = index + 1;
}
fuck.fuck();
}
}
/*
* Expected output:
* 1
* 1
* 1
* 1
*
* 0
* 0
* 0
* 0
* 0
* 0
*/
class Comp {
public void printBoolean(boolean b) {
if (b) {
System.out.println(1);
} else {
System.out.println(0);
}
}
public static void main(String[] args) {
Comp a = new Comp();
Comp b = a;
Comp c = new Comp();
Comp d = null;
a.printBoolean(a == b);
a.printBoolean(b == a);
a.printBoolean(d == null);
a.printBoolean(null == null);
System.out.write(10); /* new line */
a.printBoolean(a == c);
a.printBoolean(b == c);
a.printBoolean(a == null);
a.printBoolean(b == null);
a.printBoolean(null == a);
a.printBoolean(null == b);
}
}
\ No newline at end of file
1
1
1
1
0
0
0
0
0
0
/*
* Expected output: All primes smaller than 100.
*/
class Eratosthenes {
public int[] result;
public int count;
public void sieve(int max) {
boolean[] sieve = new boolean[max];
count = 0;
/* Initialize array. */
{
sieve[0] = false;
sieve[1] = false;
int i = 2;
while (i < max) {
sieve[i] = true;
i = i + 1;
}
}
/* Sieve of Eratosthenes algorithm. */
{
int prime = 2;
while (prime < max) {
if (sieve[prime]) {
count = count + 1;
/* Deliberately unoptimized (strength reduction is possible). */
int mult = prime;
while (prime * mult < max) {
sieve[prime * mult] = false;
mult = mult + 1;
}
}
prime = prime + 1;
}
}
result = new int[count];
{
int i = 0;
int j = 0;
while (i < max) {
if (sieve[i]) {
result[j] = i;
j = j + 1;
}
i = i + 1;
}
}
}
}
class Main {
public static void main(String[] args) throws Exception {
Eratosthenes e = new Eratosthenes();
e.sieve(100);
{
int i = 0;
while (i < e.count) {
System.out.println(e.result[i]);
i = i + 1;
}
}
}
}
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
/*
* Expected output:
*
* 2^0
* *1
* .4194304
*
* 2^-1
* *1
* .8360759
*/
class FloatArithmetic {
public int _2p8;
public int _2p11;
public int _2p16;
public int _2p23;
public int _2p24;
public int _int_min;
public int _f1_5;
public int _fNaN;
public int _seemlinglyArbitraryConstant;
public int _exponentBias;
public void init() {
_2p8 = 256;
_2p11 = 2048;
_2p16 = 65536;
_2p23 = 8388608;
_2p24 = 16777216;
_int_min = -2147483648;
_f1_5 = 1069547520;
_fNaN = -1;
_seemlinglyArbitraryConstant = 1597463007;
_exponentBias = 127;
}
public void print(int x) {
int TWO = 50;
int POWER = 94;
int MINUS = 45;
int TIMES = 42;
int DOT = 46;
int NEWLINE = 10;
int mantissa = mantissa(x) + _2p23;
int exponent = exponent(x) - _exponentBias - 23;
int sign = sign(x);
if (sign == 1) {
System.out.write(MINUS);
}
int oldExponent = exponent;
int mantissaPreDec = mantissa;
while (mantissaPreDec != 1) {
mantissaPreDec = mantissaPreDec / 2;
exponent = exponent + 1;
}
int mantissaPreDecShifted = mantissaPreDec;
while (oldExponent < exponent) {
mantissaPreDecShifted = mantissaPreDecShifted * 2;
oldExponent = oldExponent + 1;
}
System.out.write(TWO);
System.out.write(POWER);
System.out.println(exponent);
System.out.write(TIMES);
System.out.println(mantissaPreDec);
System.out.write(DOT);
System.out.println(mantissa % mantissaPreDecShifted);
System.out.write(NEWLINE);
System.out.flush();
}
public int buildFloat(int sign, int exponent, int mantissa) {
int result = exponent * _2p23 + mantissa;
if (sign == 1) {
result = negate(result);
}
return result;
}
public int negate(int x) {
return _int_min + x;
}
public int sign(int x) {
if (x >= 0) {
return 0;
} else {
return 1;
}
}
public int exponent(int x) {
if (x < 0) {
x = negate(x);
}
return x / _2p23 % _2p8;
}
public int mantissa(int x) {
if (x < 0) {
x = negate(x);
}
return x % _2p23;
}
public int div2(int x) {
int mantissa = mantissa(x);
int exponent = exponent(x);
if (exponent == 0) {
return 0;
} else {
return buildFloat(sign(x), exponent(x) - 1, mantissa(x));
}
}
public int add(int x, int y) {
int xm = mantissa(x);
int xe = exponent(x);
int xs = sign(x);
int ym = mantissa(y);
int ye = exponent(y);
int ys = sign(y);
int exponentDiff;
int exponent;
int mantissa;
int sign;
int lm;
int sm;
if (xe > ye) {
exponentDiff = xe - ye;
exponent = xe;
sign = xs;
lm = xm;
sm = ym;
} else {
exponentDiff = ye - xe;
exponent = ye;
lm = ym;
sm = xm;
if (xe < ye || xm < ym) {
sign = ys;
} else {
sign = xs;
}
}
lm = lm + _2p23;
sm = sm + _2p23;
while (exponentDiff > 0) {
sm = sm / 2;
exponentDiff = exponentDiff - 1;
}
if (xs == ys) {
mantissa = lm + sm;
} else {
mantissa = lm - sm;
}
if (mantissa / _2p24 == 1) {
mantissa = mantissa / 2;
mantissa = mantissa % _2p23;
exponent = exponent + 1;
} else {
mantissa = mantissa % _2p23;
}
return buildFloat(sign, exponent, mantissa);
}
public int square(int x) {
return mult(x, x);
}
public int mult(int x, int y) {
int xm = mantissa(x);
int xe = exponent(x);
int xs = sign(x);
int ym = mantissa(y);
int ye = exponent(y);
int ys = sign(y);
xm = xm + _2p23;
ym = ym + _2p23;
int xm_lo = xm % _2p11;
int ym_lo = ym % _2p11;
int xm_hi = xm / _2p11;
int ym_hi = ym / _2p11;
int mantissa_lo = xm * ym;
int mantissa_hi = xm_hi * ym_hi
+ xm_hi * ym_lo / _2p11
+ xm_lo * ym_hi / _2p11
+ (xm_lo * ym_hi + xm_lo * ym_hi + mantissa_lo / _2p11) / _2p11;
int exponent = xe + ye - _exponentBias;
int mantissa = mantissa_hi / 2;
if (mantissa / _2p24 == 1) {
mantissa = mantissa / 2;
mantissa = mantissa % _2p23;
exponent = exponent + 1;
} else {
mantissa = mantissa % _2p23;
}
return buildFloat((xs + ys) % 2, exponent, mantissa);
}
public int invSqrt(int num) {
if (num <= 0) {
return _fNaN;
}
int x2 = div2(num);
int y = _seemlinglyArbitraryConstant - num / 2;
y = mult(y, add(_f1_5, negate(mult(x2, square(y)))));
return y;
}
public static void main(String[] args) {
FloatArithmetic fa = new FloatArithmetic();
fa.init();
fa.print(fa.mult(1065353216, 1069547520)); /* 1.0 * 1.5 */
fa.print(fa.invSqrt(1065353216)); /* 1 / sqrt(1.0) */
}
}
2^0
*1
.4194304
2^-1
*1
.8360759
/* Expected output:
*
* 0
*
* 0
* 1
*
* 0
* 1
* 2
*
* 0
* 1
* 2
* 3
*
* 0
* 1
* 2
* 3
* 4
*
*/
class Test {
public static void main(String[] args) {
int[][] arr = new int[5][];
{
int i = 0;
while (i < 5) {
arr[i] = new int[i + 1];
i = i + 1;
}
}
{
int i = 0;
while (i < 5) {
int j = 0;
while (j < i + 1) {
arr[i][j] = j;
j = j + 1;
}
i = i + 1;
}
}
{
int i = 0;
while (i < 5) {
int j = 0;
while (j < i + 1) {
System.out.println(arr[i][j]);
j = j + 1;
}
i = i + 1;
System.out.write(10); /* newline */
}
}
}
}
0
0
1
0
1
2
0
1
2
3
0
1
2
3
4
class A {
public int i;
public static void main(String[] args) {
A a = null;
int i = a.i;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment