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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值