[ACM] 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, 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 add the number in register R1 and R2, then store the result to R1. But this instruction cannot be execute directly by computer, before this instruction is executed, it must be changed to binary code which can be executed by computer. Each instruction corresponds to a 16-bit binary code. The higher 6 bits indicates the operation code, the middle 5 bits indicates the destination operator, and the lower 5 bits indicates the source operator. You can see Form 1 for more details.




15 operation code(6 bits)109destination operator code(5 bits)54source operator code(5 bits)0Form 1

In JG system there are 6 instructions which are listed in Form 2.




instructionADD Ra,RbSUB Ra,RbDIV Ra,RbMUL Ra,RbMOVE Ra,RbSET RafunctionAdd the number in register Ra and Rb, then store the result to Ra.Subtract the number in register Ra to Rb, then store the result to Ra.Divide the number in register Ra by Rb, then store the result to Ra.Mulplicate the number in register Ra and Rb, then store the result to Ra.Move the number in register Rb to Ra.Set 0 to Ra.Form 2

Operation code is generated according to Form 3.




OperationADDSUBDIVMULMOVESETOperation code000001000010000011000100000101000110Form 3

Destination operator code and source operator code is the register code of the register which is related to.

There are 31 registers in total. Their names are R1,R2,R3…,R30,R31. The register code of Ri is the last 5 bits of the number of i in the binary system. For eaxample the register code of R1 is 00001, the register code of R2 is 00010, the register code of R7 is 00111, the register code of R10 is 01010, the register code of R31 is 11111.

So we can transfer an instruction into a 16-bit binary code easyly. For example, if we want to transfer the instruction ADD R1,R2, we know the operation is ADD whose operation code is 000001, destination operator code is 00001 which is the register code of R1, and source operator code is 00010 which is the register code of R2. So we joint them to get the 16-bit binary code which is 0000010000100010.

However for the instruction SET Ra, there is no source register, so we fill the lower 5 bits with five 0s. For example, the 16-bit binary code of SET R10 is 0001100101000000

You are expected to write a program to transfer an instruction into a 16-bit binary code or vice-versa.

 


 

Input

 

Multi test cases (about 50000), every case contains two lines.

First line contains a type sign, ‘0’ or ‘1’.
 

‘1’ means you should transfer an instruction into a 16-bit binary code;

‘0’ means you should transfer a 16-bit binary code into an instruction.

For the second line.

If the type sign is ‘1’, an instruction will appear in the standard form which will be given in technical specification;
 

Otherwise, a 16-bit binary code will appear instead.

Please process to the end of file.

[Technical Specification]

The standard form of instructions is
 

ADD Ra,Rb

SUB Ra,Rb

DIV Ra,Rb

MUL Ra,Rb

MOVE Ra,Rb

SET Ra

which are also listed in the Form 2.





1a,b31


There is exactly one space after operation, and exactly one comma between Ra and Rb other than the instruction SET Ra. No other character will appear in the instruction.

 


 

Output

 

For type ‘0’,if the 16-bit binary code cannot be transferred into a instruction according to the description output “Error!” (without quote), otherwise transfer the 16-bit binary code into instruction and output the instruction in the standard form in a single line.

For type ‘1’, transfer the instruction into 16-bit binary code and output it in a single line.
 


 

Sample Input

 

   
   
1 ADD R1,R2 0 0000010000100010 0 1111111111111111

 


 

Sample Output

 

   
   
0000010000100010 ADD R1,R2 Error!

 


 

Source

 

 

解题思路:

阅读理解模拟题。指令为“Set”时要单独且特别注意。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stdlib.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;
map<string,string>z;//操作符与二进制指令相相应
map<string,int>z2;//二进制数与十进制数相相应

void prepare()
{
    z["000001"]="ADD";
    z["000010"]="SUB";
    z["000011"]="DIV";
    z["000100"]="MUL";
    z["000101"]="MOVE";
    z["000110"]="SET";
    z2["00001"]=1;z2["00010"]=2;
    z2["00011"]=3;z2["00100"]=4;
    z2["00101"]=5;z2["00110"]=6;
    z2["00111"]=7;z2["01000"]=8;
    z2["01001"]=9;z2["01010"]=10;
    z2["01011"]=11;z2["01100"]=12;
    z2["01101"]=13;z2["01110"]=14;
    z2["01111"]=15;z2["10000"]=16;
    z2["10001"]=17;z2["10010"]=18;
    z2["10011"]=19;z2["10100"]=20;
    z2["10101"]=21;z2["10110"]=22;
    z2["10111"]=23;z2["11000"]=24;
    z2["11001"]=25;z2["11010"]=26;
    z2["11011"]=27;z2["11100"]=28;
    z2["11101"]=29;z2["11110"]=30;
    z2["11111"]=31;
}

string find(string op)//寻找操作符相应的二进制指令
{
    map<string,string>::iterator i;
    for(i=z.begin();i!=z.end();i++)
    {

        if(i->second==op)
            return (i->first);
        if(i->first==op)
            return (i->second);
    }
    return "Error!";
}

string find2(int op)//寻找十进制相应的二进制
{
    map<string,int>::iterator i;
    for(i=z2.begin();i!=z2.end();i++)
    {

        if(i->second==op)
            return (i->first);
    }
    return "Error!";
}

int find22(string op)//寻找二进制数相应的十进制
{
    map<string,int>::iterator i;
    for(i=z2.begin();i!=z2.end();i++)
    {
        if(i->first==op)
            return (i->second);
    }
    return -1;
}

string op,source;//输入
string zhi;//输入
string wa="Error!";
int sigh;

int main()
{
    prepare();
    while(scanf("%d",&sigh)!=EOF)
    {
        if(sigh==1)
        {
            cin>>op>>source;
            if(op=="SET")//单独推断
            {
                cout<<"000110";
                int a=0;//提取出来十进制数
                for(int i=1;i<source.length();i++)
                    a=a*10+source[i]-'0';
                cout<<find2(a);
                cout<<"00000"<<endl;
            }
            else
            {
                string ans=find(op);
                //提取出来两个数字
                int p;
                int a1=0,a2=0;
                bool deng=0;
                for(p=0;p<source.length();p++)
                {
                    if(source[p]==',')
                        deng=1;
                    if(p>0&&deng==0)
                        a1=a1*10+source[p]-'0';
                    if(deng==1)
                        break;
                }
                p=p+2;
                for(;p<source.length();p++)
                    a2=a2*10+source[p]-'0';

                cout<<ans;
                cout<<find2(a1)<<find2(a2);
                cout<<endl;
            }
        }
        else
        {
            cin>>zhi;
            string  zhiling=zhi.substr(0,6);
            string temp=find(zhiling);
            if(temp==wa)
            {
                cout<<wa<<endl;
                continue;
            }
            string a1=zhi.substr(6,5);
            string a2=zhi.substr(11,5);
            int f1=find22(a1);
            int f2=find22(a2);
            if(temp=="SET")
            {
                if(a2!="00000"||f1==-1)
                {
                    cout<<wa<<endl;
                    continue;
                }
                cout<<temp<<" R"<<f1<<endl;
                continue;
            }
            if(f1==-1||f2==-1)//推断两个数字是否合法
            {
                cout<<wa<<endl;
                continue;
            }
            cout<<temp<<" ";
            cout<<"R"<<f1<<",R"<<f2<<endl;
        }
    }
    return 0;
}

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116126.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C语言实现PID算法:位置式PID和增量式PID[通俗易懂]

    原创者微信公众号PID算法可以说是在自动控制原理中比较经典的一套算法,在现实生活中应用的比较广泛。大学参加过电子竞赛的朋友都应该玩过电机(或者说循迹小车),我们要控制电机按照设定的速度运转,PID控制在其中起到了关键的作用。说来惭愧,大学这门课程学的不咋滴,老师讲的课基本没听进去过。直到后面接触竞赛,算是对PID有了很基础的一点点认识,直到现在工作实际应用的…

    2022年4月11日
    113
  • 摄影后期人像高端摄影后期PS修图技巧[通俗易懂]

    摄影后期人像高端摄影后期PS修图技巧[通俗易懂]先自我介绍一下,叶子,职业修图师,从事数码后期行业12余载,擅长人像后期处理,婚纱照商业化修图,热爱摄影,喜欢旅行,总是用照片讲述故事。本文会从什么是**『皮肤质感』**,要修成这样的效果需要什么前置条件以及在过程中我们需要注意哪些核心要点为基准详细展开,意在让大家彻底明白怎样才能做出商业修图的皮肤效果。全文3504字,阅读时间约9分钟,如果觉得不愿意全看的话,可以直接拉到最后看结论。不…

    2022年6月15日
    36
  • html css制作404页面,CSS3绘制404页面

    html css制作404页面,CSS3绘制404页面标题有点噱了…最近在做一个交通有关的项目,想做一个类似标志牌的404,所以就有了这个.只用的CSS3中的旋转,效果如下上代码:Error.circle{width:200px;height:200px;border-radius:200px;border:15pxsolid#B22727;}.circle>div{color:#B22727;font:bol…

    2022年7月27日
    7
  • 小型酒店管理系统毕业论文_简易酒店管理系统

    小型酒店管理系统毕业论文_简易酒店管理系统一需求:编写程序模拟酒店管理系统:预订和退订以及查看所有房间1需要有一个酒店类2需要有一个房间类3需要有一个客户端类publicclassTest{}二分析:客户端:1先打印所有房间2等待用户输入,根据输入情况判断是预订还是退订3等待用户输入房间号4调用酒店的预订/退订方法把房间号传入完成预订/退订功能…

    2022年9月25日
    3
  • shell脚本给文件重命名_linux移动或重命名文件命令

    shell脚本给文件重命名_linux移动或重命名文件命令文件A重命名为BmvAB;

    2025年10月19日
    2
  • AFL 源码分析

    AFL 源码分析AFL 作为 C C 白盒 fuzzer 的鼻祖 为后来许多优秀的 fuzzer 提供了技术支持 衍生了很多 fuzzer 工具 本文只是站在巨人的肩膀上 参考了大量的博客 重新审计了部分源码 很多细节并没有深究 但对理解 AFL 的思想还是有一定作用的

    2025年11月14日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号