此题属于比较麻烦的模拟题,比赛的时候是队友写的, 比赛结束之后自己也写了一遍,感觉对复杂模拟的掌控还是不行!
解析:
我感觉 ADD操作 和 MOV操作比较类似 所以就写在了一块,MUL操作单独写就行了。
#include<stdio.h> #include<string.h> #define maxn 100 long long cpu[4][3]; int Binary(char str[]) { int i, sum = 0; for(i=0; str[i]; i++) sum = sum*2 + str[i]-‘0‘; return sum; } int GetR() { int i, len, R; char str[maxn]; scanf("%s", str); len = strlen(str)-1; if(str[1] >= ‘A‘ && str[1] <= ‘D‘) { if(str[2] == ‘H‘) R = cpu[str[1]-‘A‘][1]; else if(str[2] == ‘L‘) R = cpu[str[1]-‘A‘][2]; else if(str[2] == ‘X‘) R = cpu[str[1]-‘A‘][0]; } else if(str[len] == ‘H‘) { str[len] = 0; sscanf(str+1,"%X", &R); } else if(str[len] == ‘B‘) { str[len] = 0; R = Binary(str+1); } else { sscanf(str+1,"%d", &R); } return R; } void MOV_ADD(int m,int n,int flag) { int R; R = GetR(); if(flag == 0) { cpu[m][n] = R; } else { cpu[m][n] += R; } if(n == 0) { cpu[m][1] = cpu[m][0]/256; cpu[m][2] = cpu[m][0]%256; } else cpu[m][0] = cpu[m][1]*256 + cpu[m][2]; } void MUL(int m,int n) { if(n == 2 || n == 1) { cpu[0][0] = cpu[m][n]*cpu[0][2]; } else { cpu[3][0] = (cpu[m][n]*cpu[0][0])/65536; cpu[0][0] = (cpu[m][n]*cpu[0][0])%65536; cpu[3][1] = cpu[3][0]/256; cpu[3][2] = cpu[3][0]%256; } cpu[0][1] = cpu[0][0]/256; cpu[0][2] = cpu[0][0]%256; } int main() { int T, t, k, x; char str[maxn], str2[maxn]; scanf("%d",&T); while(T--) { scanf("%d",&t); memset(cpu, 0, sizeof(cpu)); while(t--) { scanf("%s%2s", str,str2); k = str2[0] - ‘A‘; if(str2[1] == ‘X‘) x = 0; else if(str2[1] == ‘H‘) x = 1; else if(str2[1] == ‘L‘) x = 2; if(strcmp(str,"MOV") == 0) { MOV_ADD(k,x,0); } else if(strcmp(str,"ADD") == 0) { MOV_ADD(k,x,1); } else MUL(k,x); } printf("%lld %lld %lld %lld\n",cpu[0][0], cpu[1][0], cpu[2][0], cpu[3][0]); } return 0; }
时间: 2024-11-05 19:37:59