Instruction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 991    Accepted Submission(s): 244

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.
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.

 
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
 
直接模拟.
#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(模拟)的更多相关文章

  1. hdu 5083 有坑+字符串模拟水题

    http://acm.hdu.edu.cn/showproblem.php?pid=5083 机器码和操作互相转化 注意SET还要判断末5位不为0输出Error #pragma comment(lin ...

  2. HDU 5083 Instruction --模拟

    题意:给出汇编指令,解释出编码或者给出编码,解释出汇编指令. 解法:简单模拟,按照给出的规则一步一步来就好了,主要是注意“SET”的情况,还有要输出的东西最好放到最后一起输出,中间如果一旦不对就可以及 ...

  3. [ACM] HDU 5083 Instruction (模拟)

    Instruction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. hdu 5083 Instruction (稍比较复杂的模拟题)

    题意: 二进制指令转汇编指令,汇编指令转二进制指令. 思路: 额,条理分好,想全,思维不能乱. 代码: int findyu(char yu[50],char c){ int l=strlen(yu) ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083 题目意思:如果给出 instruction 就需要输出对应的 16-bit binary cod ...

  6. hdu 4891 模拟水题

    http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...

  7. hdu 5012 模拟+bfs

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...

  8. hdu 4669 模拟

    思路: 主要就是模拟这些操作,用链表果断超时.改用堆栈模拟就过了 #include<map> #include<set> #include<stack> #incl ...

  9. 2013杭州网络赛C题HDU 4640(模拟)

    The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. 《Cracking the Coding Interview》——第8章:面向对象设计——题目2

    2014-04-23 17:45 题目:假设有个呼叫中心,有接线员.经理.主管三种角色.如果接线员无法处理呼叫,就上传给经理:如果仍无法处理,则上传给主管.请用代码描述这一过程. 解法:第一眼觉得这题 ...

  2. 17、bootStrap组件

    1.bootStrap组件 无数可复用的组件,包括字体图标.下拉菜单.导航.警告框.弹出框等更多功能. 2.字体图标 ①不要和其他图标混合使用 ②只能对内容为空的元素起作用 3.下拉菜单 <di ...

  3. 恢复误删除表黑科技之relay log大法

      Preface       In my previous blogs,I've demonstrated several mothods of how to rescue a dropped ta ...

  4. JFinal 添加Druid插件

    第一步:添加依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</a ...

  5. Jmeter 参数化之 CSV Data Set Config 循环读取参数

    对于做接口和性能测试,个人感觉Jmeter是一个非常方便易学的工具,今天随笔记录Jmeter 参数化之 CSV Data Set Config. 首先在开始记录之前,先搞明白2个问题 1.什么是参数化 ...

  6. Python学习-day19 django基础篇

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  7. Ipython\Jupyter数据分析工具

    使用Python进行数据分析优点 1 Python大量的库为数据分析和处理提供了完整的工具集 2 比起R和Matlab等其他主要用于数据分析的编程语言,Python更全能 3 Python库一直在增加 ...

  8. 【转】通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...

  9. POJ 2891 Strange Way to Express Integers | exGcd解同余方程组

    题面就是让你解同余方程组(模数不互质) 题解: 先考虑一下两个方程 x=r1 mod(m1) x=r2 mod (m2) 去掉mod x=r1+m1y1   ......1 x=r2+m2y2   . ...

  10. 威士忌(whiskey)

    威士忌(whiskey) 题目描述 Alan 喝了假威士忌,想问你一个问题: nvliu66 推荐大家读三本书<百年孤独>.<城市发展史>.<美国大城市的生与死>, ...