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(char t[50]){
    int l=strlen(t);
    int res=0;
    rep(i,0,l-1) res*=2, res+=(t[i]-‘0‘);
    return res;
}
void print(int x){
    int t1[10];
    int c=0;
    rep(i,0,4){
        t1[++c]=(x&1);
        x>>=1;
    }
    rep2(i,c,1) printf("%d",t1[i]);
}
int main(){
    //freopen("test.in","r", stdin);

    int kind;
    map<string,string> mp1;
    mp1["ADD"]="000001";
    mp1["SUB"]="000010";
    mp1["DIV"]="000011";
    mp1["MUL"]="000100";
    mp1["MOVE"]="000101";
    mp1["SET"]="000110";
    map<string,string> mp2;
    mp2["000001"]="ADD";
    mp2["000010"]="SUB";
    mp2["000011"]="DIV";
    mp2["000100"]="MUL";
    mp2["000101"]="MOVE";
    mp2["000110"]="SET";
    while(scanf("%d",&kind)!=EOF){
        if(kind==1){
            char ins[50],yu[50];
            int ra,rb=0;
            scanf("%s%s",ins,yu);
            if(strcmp(ins,"SET")==0)
                ra=calc(yu,1,strlen(yu)-1);
            else{
                int tc=findyu(yu,‘,‘);
                ra=calc(yu,1,tc-1);
                rb=calc(yu,tc+2,strlen(yu)-tc-2);
            }
            cout<<mp1[string(ins)];
            if(ins=="SET"){
                print(ra);
                printf("00000\n");
                continue;
            }
            print(ra);
            print(rb);
            cout<<endl;
            continue;
        }
        else{
            char str[50];
            scanf("%s",str);
            if(strlen(str)!=16){
                cout<<"Error!"<<endl;
                continue;
            }
            char ope[50],rra[50],rrb[50];
            rep(i,0,5) ope[i]=str[i]; ope[6]=0;
            rep(i,6,10) rra[i-6]=str[i]; rra[5]=0;
            rep(i,11,15) rrb[i-11]=str[i]; rrb[5]=0;
            int ra=calc2(rra);
            int rb=calc2(rrb);
            if(strcmp(ope,"000110")==0){
                if(rb!=0){
                    cout<<"Error!"<<endl;
                    continue;
                }
                if(ra<1 || ra>31){
                    cout<<"Error!"<<endl;
                    continue;
                }
                printf("SET R%d\n",ra);
                continue;
            }
            if(ra<1 || ra>31 || rb<1 || rb>31){
                cout<<"Error!"<<endl;
                continue;
            }
            if(mp2[string(ope)]==""){
                cout<<"Error!"<<endl;
                continue;
            }
            cout<<mp2[string(ope)];
            printf(" R%d,R%d\n",ra,rb);
            continue;
        }
    }

    //fclose(stdin);
}
时间: 2024-10-20 19:06:05

hdu 5083 Instruction (稍比较复杂的模拟题)的相关文章

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

HDU 4941 Magical Forest _(:зゝ∠)_ 模拟题

模拟大法保平安_(:зゝ∠)_ #include <cstdio> #include <map> #include <set> #include <algorithm> using namespace std; const int N = 1; struct node{ int x, y, val; node(int a=0,int b=0,int c=0):x(a),y(b),val(c){} bool operator<(const node&am

[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 --模拟

题意:给出汇编指令,解释出编码或者给出编码,解释出汇编指令. 解法:简单模拟,按照给出的规则一步一步来就好了,主要是注意“SET”的情况,还有要输出的东西最好放到最后一起输出,中间如果一旦不对就可以及时跳出去. 其他也没什么了,只要细心点,多测几组样例就好了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath&g

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 5071 Chat (神一般的模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 题面: Chat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 2195    Accepted Submission(s): 465 Problem Description As everyone knows, DRD has

HDU 4431 Mahjong (麻将、神坑模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4431 题面: Mahjong Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4219    Accepted Submission(s): 842 Problem Description Japanese Mahjong is a fou

BestCoder15 1002.Instruction(hdu 5083) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083 题目意思:如果给出 instruction 就需要输出对应的 16-bit binary code,给出16-bit binary code 就需要输出对应的instruction. 由于不会截取的技巧,代码量非常可观 = =,所以说,一直很讨厌做模拟题!!! 留下这代码,纪念一个代码还是不够精简的自己!!!内存和时间还能接受,也比较容易理解,不过好多重复代码= =.以下这个可以代码可以忽略,