[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
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.
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.
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.
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.
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.
1≤a,b≤31
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.
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.
1
ADD R1,R2
0
0000010000100010
0
1111111111111111
0000010000100010
ADD R1,R2
Error!
解题思路:
阅读理解模拟题。指令为“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;
}
[ACM] HDU 5083 Instruction (模拟)的更多相关文章
- HDU 5083 Instruction --模拟
题意:给出汇编指令,解释出编码或者给出编码,解释出汇编指令. 解法:简单模拟,按照给出的规则一步一步来就好了,主要是注意“SET”的情况,还有要输出的东西最好放到最后一起输出,中间如果一旦不对就可以及 ...
- hdu 5083 Instruction (稍比较复杂的模拟题)
题意: 二进制指令转汇编指令,汇编指令转二进制指令. 思路: 额,条理分好,想全,思维不能乱. 代码: int findyu(char yu[50],char c){ int l=strlen(yu) ...
- HDU 5083 Instruction(字符串处理)
Problem Description Nowadays, Jim Green has produced a kind of computer called JG. In his computer, ...
- BestCoder15 1002.Instruction(hdu 5083) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083 题目意思:如果给出 instruction 就需要输出对应的 16-bit binary cod ...
- hdu 5083 有坑+字符串模拟水题
http://acm.hdu.edu.cn/showproblem.php?pid=5083 机器码和操作互相转化 注意SET还要判断末5位不为0输出Error #pragma comment(lin ...
- HDU 4121 Xiangqi 模拟题
Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- HDU 2568[前进]模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2568 关键思想:傻傻地模拟 代码如下: #include<iostream> using ...
- hdu 4964 恶心模拟
http://acm.hdu.edu.cn/showproblem.php?pid=4964 给定语句,按照语法翻译html并输出. 就是恶心的模拟,递归搞就行了 处理id和class时,在一个'&g ...
随机推荐
- 「Foundation」集合
一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向对象的形式操纵对象,是不可变数组 ...
- linux ln 命令(转载)
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...
- Dojo实现Tabs页报错(三)
用Dojo实现tab页的过程中,没有引用“on.js”,但是firebug调试时一直提示如下错误: on.js源码如下: define(["./has!dom-addeventlistene ...
- 解析ASP.NET Mvc开发之删除修改数据
目录: 1)从明源动力到创新工场这一路走来 2)解析ASP.NET WebForm和Mvc开发的区别 3)解析ASP.NET Mvc开发之查询数据实例 4)解析ASP.NET Mvc开发之EF延迟加载 ...
- Lake Counting(poj 2386)
题目描述: Description Due to recent rains, water has pooled in various places in Farmer John's field, wh ...
- AutoCAD 2013官方简体中文破解版(32 / 64位),带激活码和注册机
AutoCAD 2014下载地址:http://ideapad.zol.com.cn/61/160_603697.html 安装及破解方法:(注册机下载在下方) 1.安装Autodesk AutoCA ...
- spring jdbc 笔记3
spring JDBC 加入对commons-dbcp spring-jdbc spring-tx的依赖 1.数据源的配置 获取数据源在spring中的Bean管理默认已经是单例模式 关闭数据源d ...
- 十句话教你学会Linux数据流重定向
1.看到重定向一下子就想起了web里面的redirect,没错,但是Linux数据流重定向的作用不是跳到另一个网页,而是用来存储重要的屏幕信息.将不必要的屏幕信息输出到文件里或者“黑洞”里.将错误信息 ...
- Android网络开发之用tcpdump抓包
Android开发过程中,当涉及到网络通信的时候,有一些字段须要抓包获取.我之前由于SSDP设备发现的包头格式没有写对,经过抓包分析和标准包头对照发现了这个困扰我非常久的问题.总之,掌握在Androi ...
- linux 内核头文件 linux kernel header
概述:在进行有关系统软件的安装的时候(编译一个新的驱动,或者安装一个系统级别的测试工具,例如systemtap),经常需要重新编译内核,相应的问题往往与内核头文件有关.那么,什么是内核头文件,为什么需 ...