【HDOJ】1341 Simple Computers
注意PC要与31.
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 40 6 #define MAXL 10 7 8 char mem[MAXN][MAXL]; 9 char ans[MAXL]; 10 11 int getv(char s[], int i, int j) { 12 int ret = 0; 13 14 while (i < j) { 15 ret = (ret<<1) + s[i] - '0'; 16 ++i; 17 } 18 19 return ret; 20 } 21 22 void store(char s[], unsigned char acc) { 23 int i = 0; 24 int v = (int)acc; 25 int mask = 0x80; 26 27 for (i=0; i<8; ++i) { 28 if (v & mask) { 29 s[i] = '1'; 30 } else { 31 s[i] = '0'; 32 } 33 mask >>= 1; 34 } 35 s[i] = '\0'; 36 } 37 38 void solve() { 39 int op, addr; 40 int tmp; 41 bool nhalt = true; 42 unsigned char acc = 0; 43 int pc = 0; 44 45 while (nhalt) { 46 pc &= 0x1f; 47 op = getv(mem[pc], 0, 3); 48 addr = getv(mem[pc], 3, 8); 49 ++pc; 50 switch (op) { 51 case 0: 52 store(mem[addr], acc); 53 break; 54 55 case 1: 56 tmp = getv(mem[addr], 0, 8); 57 acc = (unsigned char) tmp; 58 break; 59 60 case 2: 61 if (acc == 0) 62 pc = addr; 63 break; 64 65 case 3: 66 break; 67 68 case 4: 69 --acc; 70 break; 71 72 case 5: 73 ++acc; 74 break; 75 76 case 6: 77 pc = addr; 78 break; 79 80 case 7: 81 nhalt = false; 82 break; 83 84 default: 85 break; 86 } 87 } 88 89 store(ans, acc); 90 } 91 92 int main() { 93 int i; 94 95 #ifndef ONLINE_JUDGE 96 freopen("data.in", "r", stdin); 97 #endif 98 99 while (1) { 100 i = 0; 101 if (scanf("%s", mem[i++]) == EOF) 102 break; 103 while (i < 32) 104 scanf("%s", mem[i++]); 105 solve(); 106 printf("%s\n", ans); 107 } 108 109 return 0; 110 }