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;
}

时间: 2024-10-03 21:54:14

HDU 5083 Instruction --模拟的相关文章

[ACM] HDU 5083 Instruction (模拟)

Instruction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 347    Accepted Submission(s): 101 Problem Description Nowadays, Jim Green has produced a kind of computer called JG. In his computer

hdu 5083 Instruction (稍比较复杂的模拟题)

题意: 二进制指令转汇编指令,汇编指令转二进制指令. 思路: 额,条理分好,想全,思维不能乱. 代码: int findyu(char yu[50],char c){ int l=strlen(yu); rep(i,0,l-1) if(c==yu[i]) return i; } int calc(char t[50],int x,int k){ int res=0; rep(i,x,x+k-1) res*=10, res+=(t[i]-'0'); return res; } int calc2(

hdu 5083 Instruction(Bestcoder Round #15)

Instruction                                                               Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 327    Accepted Submission(s): 94 Problem Description Nowadays, Jim Gre

HDU 5083 Instruction(字符串处理)

Problem Description Nowadays, Jim Green has produced a kind of computer called JG. In his computer, the instruction is represented by binary code. However when we code in this computer, we use some mnemonic symbols. For example, ADD R1, R2 means to a

hdu 4964 Emmet(模拟)

题目链接:hdu 4964 Emmet 题目大意: 给定语句,按照语法翻译并输出. 解题思路:用递归模拟文法分析,主要注意几点: 括号并且的情况:(fuck)(you) 括号嵌套的情况:((fuck.you)) 优先输出id,然后是class(题目中有说) 乘法的部分:fuck*2>you*3 (每次执行fuck时,you的地方同样被执行了3次) 其他跑出样例基本没问题,具体看代码. #include <cstdio> #include <cstring> #include

HDU 4891 简单模拟

The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1035    Accepted Submission(s): 355 Problem Description As a programming contest addict, Waybl is always happy to take part in vario

hdu 4194(模拟)

符合三者之一的则不满足规定,求不满足规定的个数.直接模拟. 1.被同一个人审查多次 2.被和自己同一组织的审查 3.被审查次数不等于k 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 17:36 5 * Filename :

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1

HDU 2100 Lovekey 模拟26进制

Problem Description XYZ-26进制数是一个每位都是大写字母的数字. A.B.C.-.X.Y.Z 分别依次代表一个0 ~ 25 的数字,一个 n 位的26进制数转化成是10进制的规则如下 A0A1A2A3-An-1 的每一位代表的数字为a0a1a2a3-an-1 ,则该XYZ-26进制数的10进制值就为 m = a0 * 26^(n-1) + a1 * 26^(n-2) + - + an-3* 26^2 + an-2*26 + an-1 一天vivi忽然玩起了浪漫,要躲在学校