题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083
题目意思:如果给出 instruction 就需要输出对应的 16-bit binary code,给出16-bit binary code 就需要输出对应的instruction。
由于不会截取的技巧,代码量非常可观 = =,所以说,一直很讨厌做模拟题!!!
留下这代码,纪念一个代码还是不够精简的自己!!!内存和时间还能接受,也比较容易理解,不过好多重复代码= =。以下这个代码可以忽略,改到晕= =。之后会补上简单容易理解版滴......
一...场.......噩..........梦!!!
78Ms 284K 5708B

1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 20 + 5; 8 const int N = 10 + 2; 9 char instruct[7][N] = {"0", "ADD", "SUB", "DIV", "MUL", "MOVE", "SET"}; 10 char num_instruct[7][N] = {"0", "000001", "000010", "000011", "000100", "000101", "000110"}; 11 12 char R[32][N] = {"0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", 13 "R9", "R10", "R11", "R12", "R13", "R14", "R15", "R16", 14 "R17", "R18", "R19", "R20", "R21", "R22", "R23", "R24", 15 "R25", "R26", "R27", "R28", "R29", "R30", "R31"}; 16 char num_R[32][N] = {"0", "00001", "00010", "00011", "00100", "00101", "00110", 17 "00111", "01000", "01001", "01010", "01011", "01100", "01101", 18 "01110", "01111", "10000", "10001", "10010", "10011", "10100", 19 "10101", "10110", "10111", "11000", "11001", "11010", "11011", 20 "11100", "11101", "11110", "11111"}; 21 22 char s1[N], s2[maxn]; 23 char s[maxn]; 24 25 int main() 26 { 27 #ifndef ONLINE_JUDGE 28 freopen("ljy.txt", "r", stdin); 29 #endif 30 31 int ask; 32 while (scanf("%d", &ask) != EOF) 33 { 34 char tmp1[N], tmp2[N]; 35 if (ask == 1) 36 { 37 int i; 38 scanf("%s%s", s1, s2); 39 for (i = 1; i < 7; i++) 40 { 41 if (!strcmp(s1, instruct[i])) 42 { 43 printf("%s", num_instruct[i]); 44 break; 45 } 46 } 47 if (!strcmp(s1, "SET")) // SET指令单独处理 48 { 49 for (i = 0; i < 32; i++) 50 { 51 if (!strcmp(s2, R[i])) 52 { 53 printf("%s00000 ", num_R[i]); 54 break; 55 } 56 } 57 } 58 else 59 { 60 int l1 = 0; 61 int len = strlen(s2); 62 for (i = 0; i < len; i++) 63 { 64 if (s2[i] == ',') 65 break; 66 tmp1[l1++] = s2[i]; 67 } 68 tmp1[l1] = '