#include #include ifstream in("computer.in"); typedef unsigned char Byte; Byte memory[32]; char* tobinary(Byte b) { static char c[9]; c[8]=0; for(int i=0; i<8; i++) { c[7-i]=(b & 01)+'0'; b/=2; } return c; } Byte getbyte(char *line) { Byte b=line[0]-'0'; for(int i=1; i<8; i++) { b*=2; b+=line[i]-'0'; } return b; } Byte compute() { Byte accu=0, pc=0; Byte inst, m=memory[0]; bool terminate=((m >> 5)==7); while(!terminate) { pc=(pc+1)%32; inst=m >> 5; m=m&0x1f; switch (inst) { case 00: memory[m]=accu; break; case 01: accu=memory[m]; break; case 02: if(!accu) pc=m; break; case 03: break; case 04: accu--; break; case 05: accu++; break; case 06: pc=m; break; case 07: terminate=true; break; } m=memory[pc]; } return accu; } int main() { char line[9]; while (in >> line) { memory[0]=getbyte(line); for(int i=1; i<32; i++) { in >> line; memory[i]=getbyte(line); } cout << tobinary(compute()) << endl; } return 0; }