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. CentOS 6.0 VNC远程桌面配置[转]

    原文出处: http://blog.haohtml.com/archives/12281 谢谢作者. 引言:必须明白:vncserver在调用的时候,会根据你的配置来启用server端的监听端口,端口 ...

  2. 《Cracking the Coding Interview》——第5章:位操作——题目3

    2014-03-19 05:57 题目:给定一个整数N,求出比N大,而且二进制表示中和N有相同个数的‘1’的最小的数,比如3是‘11’,接下来的5是‘101’,再接下来的6是‘110’. 解法:从低位 ...

  3. 玩转Node.js(二)

    玩转Node.js(二) 先来回顾上次的内容,上一次我们使用介绍了Node.js并写了第一个服务器端的Hello World程序,在这个Hello World程序中,请求自带的http模块并将其赋给h ...

  4. B树、B-树、B+树、B*树 红黑树

    转载自:http://blog.csdn.net/quitepig/article/details/8041308 B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): ...

  5. Percona-Tookit工具包之pt-slave-find

      Preface       If we want to check out how many slaves in our replication environment.We can execut ...

  6. postman导出excel出现response

    https://jingyan.baidu.com/article/915fc414559b4351394b2084.html

  7. sqlalchemy 查询姿势总结

    sqlalchemy查询使用 1.带条件查询 查询是最常用的,对于各种查询我们必须要十分清楚,首先是带条件的查询 #带条件查询 rows = session.query(User).filter_by ...

  8. Python全栈工程师(异常(基础))

    ParisGabriel                每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰     Python人工智能从入门到精通 补充:包的相对导入 只对后两种导入方式有用 ...

  9. Opencv3.2.0安装包

    这个资源是Opencv3.2.0安装包,包括Windows软件包,Android软件包,IOS软件包,还有opencv的源代码:需要的下载吧. 点击下载

  10. 1024Studio官网

    一.开发背景 在工作室成立之后,一直就想为工作室建设一个网站,这次乘着暑假有足够的空余时间,开始着手建设我们1024studio的官方网站. 二.系统设计 1.系统目标 根据网上查找的相关资料以及与工 ...