(2015年鄭州輕工業學院ACM校賽題) E 彙編原理

來源:互聯網
上載者:User

標籤:

  此題屬於比較麻煩的類比題,比賽的時候是隊友寫的, 比賽結束之後自己也寫了一遍,感覺對複雜類比的掌控還是不行!

解析:

我感覺 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 彙編原理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.