HDU 5083 Instruction(字符串处理)
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)10
operator code(5 bits)5
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">4source
operator code(5 bits)0Form
1
In JG system there are 6 instructions which are listed in Form 2.
Ra,Rb
Ra,Rb
Ra,Rb
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">MUL
Ra,RbMOVE
Ra,RbSET
Ra
ref=toolbar" width="28" height="30" alt="" style="height:10.09em; width:0px">functionAdd
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.
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">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.
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">Form
2
ref=toolbar" width="28" height="30" alt="" style="height:12em; width:0px">
Operation code is generated according to Form 3.
code
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">000010000011
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">000100
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">000101000110
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">Form
3
ref=toolbar" width="28" height="30" alt="" style="height:11.94em; width:0px">
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
ref=toolbar" width="28" height="30" alt="" style="height:4em; width:0px">
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.
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!
题意:就是有6个操作,分别有相应的二进制编码表示,看上面的表,然后仅仅有1~31的数进行加减乘除,1用00001表示,31用11111表示
这里要你翻译编码的意思;比如例子1:
1
ADD R1,R2
1代表你要把Add换成000001 然后后面R1 1的编码 00001,R2的编码00010合起来就是 0000010000100010
第二组例子就是相反的意思
不得不说的是 一定要注意 SET 操作
好了,详细解释在代码中:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 1005 char op[10][10]={"ADD","SUB","DIV","MUL","MOVE","SET"};
char opp[10][10]={"000001","000010","000011","000100","000101","000110"}; char e[33][10]={"00000","00001","00010","00011","00100", //请注意这个表非常好,就是假设相应00011,那么就是下标3
"00101","00110","00111","01000","01001",
"01010","01011","01100","01101","01110",
"01111","10000","10001","10010","10011",
"10100","10101","10110","10111","11000",
"11001","11010","11011","11100","11101",
"11110","11111"}; char a[N],b[N],c[N]; int fdd(char *a) //把Ra中的a取出来的函数
{
int i,temp=0;
int len=strlen(a); for(i=1;i<len;i++)
temp=temp*10+a[i]-'0';
return temp;
} void solve()
{
int i,j,pos; for(i=0;i<6;i++)
if(strcmp(a,opp[i])==0)
break;
pos=i; if(i==6) //推断操作数是否合格
{
printf("Error!\n");
return ;
}
if(i==5) //假设是SET相应的000110 就单独处理
{
if(strcmp(c,e[0])!=0||strcmp(b,e[0])==0) //必须满足c串相应的是0而且b串相应的数!=0,否者Error
{
printf("Error!\n");
return ;
}
for(i=1;i<32;i++)
if(strcmp(b,e[i])==0)
break;
printf("SET R%d\n",i);
return ;
} for(i=1;i<32;i++)
if(strcmp(b,e[i])==0)
break;
j=i;
for(i=1;i<32;i++)
if(strcmp(c,e[i])==0)
break; if(i>=32||j>=32) //推断b,c串是否是e数组里的,换而言之是否合格(大于31就是不合格的)
{
printf("Error!\n");
return ;
}
printf("%s R%d,R%d\n",op[pos],j,i);
} int main()
{
int i,j,x;
while(~scanf("%d",&x))
{
if(x==1)
{
scanf("%s%s",a,b);
if(strcmp(a,op[5])==0) //假设是SET单独处理
{
int pos=fdd(b);
printf("%s%s00000\n",opp[5],e[pos]);
continue;
}
int len=strlen(b); //分成3段 a b c 分别代表 哪一种操作,Ra Rb
for(i=0;i<len;i++)
if(b[i]==',')
break;
b[i]='\0';
j=0;
i++;
for(i;i<len;i)
c[j++]=b[i++];
c[j]='\0'; for(i=0;i<6;i++)
if(strcmp(a,op[i])==0)
break; printf("%s",opp[i]);
int pos;
pos=fdd(b); printf("%s",e[pos]);
pos=fdd(c);
printf("%s\n",e[pos]);
}
else
{
scanf("%s",a);
j=0;
for(i=6;i<=10;i++)
b[j++]=a[i]; j=0;
for(i;i<=15;i++)
c[j++]=a[i]; a[6]='\0';
b[5]='\0';
c[5]='\0'; solve();
}
} return 0; }
HDU 5083 Instruction(字符串处理)的更多相关文章
- [ACM] HDU 5083 Instruction (模拟)
Instruction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 5083 Instruction --模拟
题意:给出汇编指令,解释出编码或者给出编码,解释出汇编指令. 解法:简单模拟,按照给出的规则一步一步来就好了,主要是注意“SET”的情况,还有要输出的东西最好放到最后一起输出,中间如果一旦不对就可以及 ...
- hdu 5083 Instruction (稍比较复杂的模拟题)
题意: 二进制指令转汇编指令,汇编指令转二进制指令. 思路: 额,条理分好,想全,思维不能乱. 代码: int findyu(char yu[50],char c){ int l=strlen(yu) ...
- HDU 1274 展开字符串 (递归+string类)
题目链接:HDU 1274 展开字符串 中文题. 左括号进入DFS函数,右括号return到上一层. 注意return回去的是这个一层递归中的括号中的字母串. AC代码: #include<st ...
- hdu 5510 Bazinga(字符串kmp)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 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 ...
- Instruction (hdu 5083)
Instruction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
随机推荐
- linq to sql之组装where条件下的'或'语句
之前遇到过类似的需求,即前台传入几个过滤条件,后台动态组装where. 例如,前台传入name='张三',age=10, 其余的字段,类似email,QQ之类的本次查询时不做过滤. 用linq to ...
- Extjs4.x treegrid,check-tree,locked getChecked() 方法错误
当在treegrid中,锁定treecolumn列的时候,是无法通过执行getView().getChecked()获取选中的节点的,这是tree的一个bug, 详见:http://www.sench ...
- Java获取mysql数据库元数据
修改后的版本: package com.genratesql.util; import java.sql.Connection; import java.sql.DatabaseMetaData; i ...
- 再谈git的http服务-权限控制gitweb版(未成功)
截至目前,对gitweb的掌握还没达到最终目标,仅仅实现了通过浏览器来浏览项目,通过git命令仍然未能clone项目.但仍然要记录下来,主要是因为打算暂时放弃这条路,而所收获的一些经验还是要记录下来. ...
- Git安装遇到的问题fatal: Could not read from remote repository.的解决办法
转自:https://blog.csdn.net/huahua78/article/details/52330792 查看远端地址 git remote –v 查看配置 git config --li ...
- 高通 双MIC 设置
android O中设置双MIC降噪,需要在build.prop添加属性"ro.vendor.audio.sdk.fluencetype"属性. 属性值位于hardware/qco ...
- ubuntu 系统启动异常之无登录界面和版本号启动四个点的地方卡住
zlib 搞的鬼,还没结局,由于rtmpdump 安装需要安装独立zlib库,装完后重启,完了吓一跳,卡住,尼玛这一年的代码都在里面啊!!! ldd /usr/sbin/python 查询库依赖zli ...
- ARM mbed平台WIZwiki-W7500使用说明
ARM mbed IDE 是ARM内核微控制器的在线开发工具,其站点是:http://developer.mbed.org. 站点提供了在线编译器,不须要本地安装编译器就可以进行开发,因此没有地点.时 ...
- EF5+MVC4系列(10) mvc的布局页面 _ViewStart.Cshtml
当客户端请求 /Product/Index的时候, 如果在视图的根目录下有 _ViewStart.Cshtml 就会先执行这个,再去执行 Product文件夹下的Index视图, 如果Product文 ...
- xml根据属性去重。如csprj去重
public static void distinct(string filePath) { //1.创建XML文档对象 XmlDocument doc = new XmlDocument(); // ...