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

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

BestCoder Round #15

直接模拟.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
char opr0[][10] = {"0","ADD","SUB","DIV","MUL","MOVE","SET"};
char opr1[][10] = {"0","000001","000010","000011","000100","000101","000110"};
char opr2[][10] = {"0","00001","00010","00011","00100","00101","00110","00111","01000",
                   "01001","01010","01011","01100","01101","01110","01111","10000","10001","10010","10011",
                   "10100","10101","10110","10111","11000","11001","11010","11011","11100","11101","11110","11111"
                  };
int main()
{
    char s[20],s1[20],s2[100];
    int opr;
    while(scanf("%d",&opr)!=EOF)
    {
        if(opr==1)
        {
            scanf("%s %s",s,s1);
            if(strcmp(s,"ADD")==0) printf("%s",opr1[1]);
            if(strcmp(s,"SUB")==0) printf("%s",opr1[2]);
            if(strcmp(s,"DIV")==0) printf("%s",opr1[3]);
            if(strcmp(s,"MUL")==0) printf("%s",opr1[4]);
            if(strcmp(s,"MOVE")==0) printf("%s",opr1[5]);
            if(strcmp(s,"SET")==0) printf("%s",opr1[6]);
            int len = strlen(s1);
            int sum = 0;
            if(strcmp(s,"SET")==0)
            {
                for(int i=1; i<len; i++) sum = sum*10+s1[i]-‘0‘;
                printf("%s",opr2[sum]);
                printf("00000");
            }
            else
            {
                int i;
                for(i=1; i<len&&s1[i]!=‘,‘; i++) sum = sum*10+s1[i]-‘0‘;
                printf("%s",opr2[sum]);
                sum = 0;
                i+=2;
                for(; i<len; i++) sum = sum*10+s1[i]-‘0‘;
                printf("%s",opr2[sum]);
            }
            printf("\n");
        }
        else
        {
            scanf("%s",s2);
            char s3[100],s4[100],s5[100];
            int len = strlen(s2);
            if(strlen(s2)!=16)
            {
                printf("Error!\n");
                continue;
            }
            int cnt = 0,cnt1=0,cnt2=0;
            for(int i=0; i<6&&i<len; i++) s3[cnt++] = s2[i];
            s3[cnt]=‘\0‘;
            for(int i=6; i<11&&i<len; i++) s4[cnt1++] = s2[i];
            s4[cnt1]=‘\0‘;
            for(int i=11; i<=15&&i<len; i++) s5[cnt2++] = s2[i];
            s5[cnt2]=‘\0‘;
            bool flag = false;
            int t1=7;
            for(int i=1; i<=6; i++)
            {
                if(strcmp(s3,opr1[i])==0)
                {
                    t1 = i;
                    break;
                }
            }
            if(t1==7) flag = true;
            int t2 = 32;
            for(int i=1; i<32; i++)
            {
                if(strcmp(s4,opr2[i])==0)
                {
                    t2 = i;
                    break;
                }
            }
            if(t2==32) flag = true;
            if(t1==6)
            {
                if(strcmp(s5,"00000")!=0) flag = true;
                if(!flag) printf("%s R%d\n",opr0[t1],t2);
                else printf("Error!\n");
            }
            else
            {
                int t3 = 32;
                for(int i=1; i<32; i++)
                {
                    if(strcmp(s5,opr2[i])==0)
                    {
                        t3 = i;
                        break;
                    }
                }
                if(t3==32) flag = true;
                if(flag) printf("Error!\n");
                else printf("%s R%d,R%d\n",opr0[t1],t2,t3);
            }
        }
    }
    return 0;
}
时间: 2024-12-10 18:20:03

hdu 5083(模拟)的相关文章

HDU 4930 模拟

Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 266    Accepted Submission(s): 87 Problem Description Fighting the Landlords is a card game which has been a heat for yea

HDU 5083 Instruction --模拟

题意:给出汇编指令,解释出编码或者给出编码,解释出汇编指令. 解法:简单模拟,按照给出的规则一步一步来就好了,主要是注意“SET”的情况,还有要输出的东西最好放到最后一起输出,中间如果一旦不对就可以及时跳出去. 其他也没什么了,只要细心点,多测几组样例就好了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath&g

[ACM] HDU 5083 Instruction (模拟)

Instruction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 347    Accepted Submission(s): 101 Problem Description Nowadays, Jim Green has produced a kind of computer called JG. In his computer

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

题意: 二进制指令转汇编指令,汇编指令转二进制指令. 思路: 额,条理分好,想全,思维不能乱. 代码: int findyu(char yu[50],char c){ int l=strlen(yu); rep(i,0,l-1) if(c==yu[i]) return i; } int calc(char t[50],int x,int k){ int res=0; rep(i,x,x+k-1) res*=10, res+=(t[i]-'0'); return res; } int calc2(

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083 题目意思:如果给出 instruction 就需要输出对应的 16-bit binary code,给出16-bit binary code 就需要输出对应的instruction. 由于不会截取的技巧,代码量非常可观 = =,所以说,一直很讨厌做模拟题!!! 留下这代码,纪念一个代码还是不够精简的自己!!!内存和时间还能接受,也比较容易理解,不过好多重复代码= =.以下这个可以代码可以忽略,

hdu 1022 模拟栈

其实就是模拟一下栈啦. 1 #include <iostream> 2 using namespace std; 3 4 const int N = 10; 5 char o1[N]; 6 char o2[N]; 7 char s[N]; 8 int ans[N * 2]; 9 10 int main () 11 { 12 int n; 13 while ( cin >> n ) 14 { 15 cin >> o1 >> o2; 16 int top = 0

hdu 4054 模拟 练习十六进制输出

http://acm.hdu.edu.cn/showproblem.php?pid=4054 貌似一般区域赛都会有一道水题 这道题PE了一次  因为输出每个数其实是两个位 如果用空格补齐的话  应该用两个空格 我用了一个空格,,, 学到: 1.%x  十六进制输出  可以输出整型,字符型等等 2.%02x  保证两位 而且会输出先导0的两位 //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstd

hdu 5246(模拟)

题解:直接模拟 #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 10005; long long n, m, k; long long s[N]; int main() { int t, cas = 1; scanf("%d", &t); while (t--) { scanf("%lld%l

hdu 5386 模拟

想明白以后会发现其实就是模拟... 因为每次只能给一整行或者一整列赋值,所以目标矩阵一开始一定有一行或者一列上数字都是相同的,然后找到对应的操作,把那一行或者那一列标记为访问过.然后新的矩阵也一定能找到一行或者一列数字都相同,再找到相应的操作,标记那一行或者那一列,依次类推,然后没有用到的操作随便找个顺序就好了.其实初始矩阵并没有什么用...... 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio>