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 ...
随机推荐
- USACO Section1.5 Superprime Rib 解题报告
sprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.2 Palindromic Squares 解题报告
palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...
- 基于mysqldump备份集来恢复某个误操作的表(drop,truncate)
Preface How to rescue a dropped or truncated table online?Dropping or truncating is ddl oper ...
- mysql Access denied for user 'root'@'localhost'问题解决
问题描述: 系统安装mysql的过程中,没有提示配置用户名和密码相关的信息,安装完毕后,登录报错. 表现现象为: mysql -u root -p [输入root密码] 界面提示: ERROR 169 ...
- fragment中的WebView返回上一页
public final class Text1Fm extends Fragment { static WebView mWeb; private View mContentView; privat ...
- poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14113 Accepted: 6260 Descr ...
- Access连接字符串
Access2007没有密码连接: <connectionStrings> <add name="myconn" connectionString="P ...
- nagios客户端安装
在被监控服务器(Linux/unix)上安装Nagios-plugins和nrpe 1.添加用户 1 2 ; html-script: false ]/usr/sbin/useradd -m na ...
- 【bzoj2879】[Noi2012]美食节 费用流+动态加边
原文地址:http://www.cnblogs.com/GXZlegend 题目描述 CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M自然不愿意错过这场盛宴.他 ...
- 用canvas实现鼠标拖动绘制矩形框
需要用到jCanvas插件和jQuery. jCanvas下载:https://raw.githubusercontent.com/caleb531/jcanvas/master/jcanvas.mi ...