hdu 5083(模拟)
Instruction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 991 Accepted Submission(s): 244
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.
In JG system there are 6 instructions which are listed in Form 2.
Operation code is generated according to Form 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.
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.
ADD R1,R2
0
0000010000100010
0
1111111111111111
ADD R1,R2
Error!
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
char opr0[][] = {"","ADD","SUB","DIV","MUL","MOVE","SET"};
char opr1[][] = {"","","","","","",""};
char opr2[][] = {"","","","","","","","","",
"","","","","","","","","","","",
"","","","","","","","","","","",""
};
int main()
{
char s[],s1[],s2[];
int opr;
while(scanf("%d",&opr)!=EOF)
{
if(opr==)
{
scanf("%s %s",s,s1);
if(strcmp(s,"ADD")==) printf("%s",opr1[]);
if(strcmp(s,"SUB")==) printf("%s",opr1[]);
if(strcmp(s,"DIV")==) printf("%s",opr1[]);
if(strcmp(s,"MUL")==) printf("%s",opr1[]);
if(strcmp(s,"MOVE")==) printf("%s",opr1[]);
if(strcmp(s,"SET")==) printf("%s",opr1[]);
int len = strlen(s1);
int sum = ;
if(strcmp(s,"SET")==)
{
for(int i=; i<len; i++) sum = sum*+s1[i]-'';
printf("%s",opr2[sum]);
printf("");
}
else
{
int i;
for(i=; i<len&&s1[i]!=','; i++) sum = sum*+s1[i]-'';
printf("%s",opr2[sum]);
sum = ;
i+=;
for(; i<len; i++) sum = sum*+s1[i]-'';
printf("%s",opr2[sum]);
}
printf("\n");
}
else
{
scanf("%s",s2);
char s3[],s4[],s5[];
int len = strlen(s2);
if(strlen(s2)!=)
{
printf("Error!\n");
continue;
}
int cnt = ,cnt1=,cnt2=;
for(int i=; i<&&i<len; i++) s3[cnt++] = s2[i];
s3[cnt]='\0';
for(int i=; i<&&i<len; i++) s4[cnt1++] = s2[i];
s4[cnt1]='\0';
for(int i=; i<=&&i<len; i++) s5[cnt2++] = s2[i];
s5[cnt2]='\0';
bool flag = false;
int t1=;
for(int i=; i<=; i++)
{
if(strcmp(s3,opr1[i])==)
{
t1 = i;
break;
}
}
if(t1==) flag = true;
int t2 = ;
for(int i=; i<; i++)
{
if(strcmp(s4,opr2[i])==)
{
t2 = i;
break;
}
}
if(t2==) flag = true;
if(t1==)
{
if(strcmp(s5,"")!=) flag = true;
if(!flag) printf("%s R%d\n",opr0[t1],t2);
else printf("Error!\n");
}
else
{
int t3 = ;
for(int i=; i<; i++)
{
if(strcmp(s5,opr2[i])==)
{
t3 = i;
break;
}
}
if(t3==) flag = true;
if(flag) printf("Error!\n");
else printf("%s R%d,R%d\n",opr0[t1],t2,t3);
}
}
}
return ;
}
hdu 5083(模拟)的更多相关文章
- hdu 5083 有坑+字符串模拟水题
http://acm.hdu.edu.cn/showproblem.php?pid=5083 机器码和操作互相转化 注意SET还要判断末5位不为0输出Error #pragma comment(lin ...
- HDU 5083 Instruction --模拟
题意:给出汇编指令,解释出编码或者给出编码,解释出汇编指令. 解法:简单模拟,按照给出的规则一步一步来就好了,主要是注意“SET”的情况,还有要输出的东西最好放到最后一起输出,中间如果一旦不对就可以及 ...
- [ACM] HDU 5083 Instruction (模拟)
Instruction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 5083 Instruction (稍比较复杂的模拟题)
题意: 二进制指令转汇编指令,汇编指令转二进制指令. 思路: 额,条理分好,想全,思维不能乱. 代码: int findyu(char yu[50],char c){ int l=strlen(yu) ...
- BestCoder15 1002.Instruction(hdu 5083) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083 题目意思:如果给出 instruction 就需要输出对应的 16-bit binary cod ...
- hdu 4891 模拟水题
http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...
- hdu 5012 模拟+bfs
http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...
- hdu 4669 模拟
思路: 主要就是模拟这些操作,用链表果断超时.改用堆栈模拟就过了 #include<map> #include<set> #include<stack> #incl ...
- 2013杭州网络赛C题HDU 4640(模拟)
The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- Android Studio快速的接受一个项目
1.关键词搜索法,搜索这个词,然后仔细的去找找.肯定可以找到的,虽然可能会有一些奔波. 2.Android device moniter ,可以抓屏幕,看布局,而且可以看到资源id.看到资源id,你说 ...
- Result Maps collection does not contain value for xxxx
这是mybatis查询返回值的错误,我在做一个查询数值的方法,但是我是这样写的: <select id="findSize" resultMap="long&quo ...
- 让NVelocity做更多的事,VS Extension+NVelocity系列
我不知道园子里到底有多少人喜欢使用NVelocity这个模板引擎,其实说实话,如果现在让我选,我对Razor的喜好要比NVelocity或者T4等等的模板引擎更多一些,当然了,个人看法而已.只是我在公 ...
- VSX-4 VSXTra
要介绍VSXTra项目不是一个简单的事情. 而且要在上面进行扩展,删减就更不容易. 源码分析的资料几乎没有,可怜的几个示例项目,相较而言,英文已经不是阻碍我前进的步伐了. 本篇只是简单的分析,对于已经 ...
- 剑指Offer - 九度1367 - 二叉搜索树的后序遍历序列
剑指Offer - 九度1367 - 二叉搜索树的后序遍历序列2013-11-23 03:16 题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出 ...
- git :.gitigrone文件不生效的解决办法
真正的原因是.gitignore只能忽略那些尚未被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的.一个简单的解决方法就是先把本地缓存删除(改变成未track状 ...
- U-Net卷积神经网络
由于项目需要,用U-NET跑一个程序来对医学影像进行分割(segmentation),因此跑去看了下这篇论文(paper),下面会介绍一下U-Net的框架及要点,如果哪里有写的不对的,或者好的建议,欢 ...
- php设计模式 工厂模式和单例
1.单例模式//让该类在外界无法造对象//让外界可以造一个对象,做一个静态方法返回对象//在类里面通过让静态变量控制返回对象只能是一个. class cat{ public $name; privat ...
- 【bzoj3829】[Poi2014]FarmCraft 贪心
原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html 题目描述 In a village called Byteville, there are ...
- 【转】VS常用快捷键
每次在网上搜关于VS有哪些常用快捷键的时候,出来的永远是一串长的不能再长的列表,完全没体现出“常用”二字,每次看完前面几个就看不下去了,相信大家都 有这种感觉.其实我们平时用的真的只有很少的一部分,借 ...