Fork me on GitHub

HDU 5083 Instruction --模拟

题意:给出汇编指令,解释出编码或者给出编码,解释出汇编指令。

解法:简单模拟,按照给出的规则一步一步来就好了,主要是注意“SET”的情况,还有要输出的东西最好放到最后一起输出,中间如果一旦不对就可以及时跳出去。

其他也没什么了,只要细心点,多测几组样例就好了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define N 100007

string op[8] = {"ADD","SUB","DIV","MUL","MOVE","SET"};
string bop[8] = {"000001","000010","000011","000100","000101","000110"};
string bina[36],itostr[35];

void init()
{
    for(int i=1;i<32;i++)
    {
        string now = "";
        int tmp = i;
        for(int j=0;j<5;j++)
        {
            if(tmp%2) now += "1";
            else      now += "0";
            tmp /= 2;
        }
        reverse(now.begin(),now.end());
        bina[i] = now;         //i的二进制形式

        tmp = i;
        string no = "";
        while(tmp)
        {
            no += tmp%10+'0';
            tmp/=10;
        }
        reverse(no.begin(),no.end());
        itostr[i] = no;        //i的十进制形式
    }
}

int main()
{
    string num,A;
    char ss[3],R1,R2;
    int sign,i,j,a,b;
    init();
    while(scanf("%d",&sign)!=EOF)
    {
        if(sign == 1)
        {
            cin>>A;
            getchar();
            if(A != "SET")
                scanf("%c%d,%c%d",&R1,&a,&R2,&b);
            else
                scanf("%c%d",&R1,&a);
            for(i=0;i<7;i++)
            {
                if(A == op[i])
                    break;
            }
            if(i == 7) { puts("Error!"); continue; }
            if(A != "SET")
            {
                if(R1 != 'R' || R2 != 'R' || a <= 0 || a >= 32 || b <= 0 || b >=32)
                {
                    puts("Error!");
                    continue;
                }
                cout<<bop[i]<<bina[a]<<bina[b]<<endl;
            }
            else
            {
                if(R1 != 'R'|| a <= 0 || a >= 32)
                {
                    puts("Error!");
                    continue;
                }
                cout<<bop[i]<<bina[a]<<"00000"<<endl;
            }
        }
        else
        {
            cin>>num;
            string A,B,C;
            string oA,oB,oC;
            A = num.substr(0,6);
            B = num.substr(6,5);
            C = num.substr(11,5);
            for(i=0;i<7;i++)
            {
                if(A == bop[i])
                    break;
            }
            if(i == 7) { puts("Error!"); continue; }
            oA = op[i];
            if(op[i] != "SET")
            {
                for(i=0;i<32;i++)
                {
                    if(B == bina[i])
                        break;
                }
                if(i == 32) { puts("Error!"); continue; }
                oB = "R"+itostr[i];
                for(i=0;i<32;i++)
                {
                    if(C == bina[i])
                        break;
                }
                if(i == 32) { puts("Error!"); continue; }
                oC = "R"+itostr[i];
                cout<<oA<<" "<<oB<<","<<oC<<endl;
            }
            else
            {
                if(C != "00000") { puts("Error!"); continue;}
                for(i=0;i<32;i++)
                {
                    if(B == bina[i])
                        break;
                }
                if(i == 32) { puts("Error!"); continue; }
                oB = "R"+itostr[i];
                cout<<oA<<" "<<oB<<endl;
            }
        }
    }
    return 0;
}
View Code

 

posted @ 2014-10-25 23:25  whatbeg  阅读(233)  评论(0编辑  收藏  举报