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. [Python3.x]python3.5实现socket通讯(TCP)

    TCP连接: tcp是面向连接的一个协议,意味着,客户端和服务器开发发送数据之前,需要先握手创建一个TCP连接.TCP连接的一端与客户端套接字相互联系,另一端与服务器套接字相联系.当创建该TCP连接的 ...

  2. Github前端项目排名

      Github前端项目排名(2016-04-04) 一.前言 近几年前端技术日新月异,从 RequireJS 到 AngularJS 再到 React,似乎每天都有新的技术诞生.而大神们总能第一时间 ...

  3. 发布“豪情”设计的新博客皮肤-darkgreentrip

    豪情 (http://www.cnblogs.com/jikey/)是一名在上海的前端开发人员,长期驻扎在园子里.他为大家设计了一款新的博客皮肤——darkgreentrip. 以下是该博客皮肤的介绍 ...

  4. Remote使用出现的问题及解决办法

    最近尝试跟着虫师的OP模式所写的bbs代码,应用自己的项目尝试修改,在第一步Remote启动Firefox上便出错,当前selenium2.53,firefox47.1,selenium server ...

  5. Linux认知之旅【06 图形界面上的各种折腾】!

    玩linux免不了折腾,不折腾对不起linux 初次接触, 总会接触到绚丽的linux桌面! 但是随之而来的桌面优化,字体安装,操作习惯都需要一一适应

  6. cloud-init简介及组件说明

    http://cloudinit.readthedocs.io/en/latest/topics/examples.html介绍:    cloud-init是专为云环境中虚拟机的初始化而开发的工具, ...

  7. java 中基本类型与字符串之间的互相转换

    1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...

  8. 201621123034 《Java程序设计》第12周学习总结

    作业12-流与文件 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车 ...

  9. css控制文字模糊

    *{ color: transparent; text-shadow: #111 0 0 5px; }

  10. BI商业智能培训系列——(一)概述

    简介: Business Intelligence,简称 BI. 商业智能,是指用现代的技术进行数据分析,以实现商业价值,这些技术包括数据仓库技术,线上分析处理技术,数据挖掘,数据展现技术等. 以往的 ...