標籤:
此題屬於比較麻煩的類比題,比賽的時候是隊友寫的, 比賽結束之後自己也寫了一遍,感覺對複雜類比的掌控還是不行!
解析:
我感覺 ADD操作 和 MOV操作比較類似 所以就寫在了一塊,MUL操作單獨寫就行了。
#include<stdio.h>#include<string.h>#define maxn 100long 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;}
(2015年鄭州輕工業學院ACM校賽題) E 彙編原理