Instruction (hdu 5083)

Instruction

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

Total Submission(s): 343    Accepted Submission(s): 99

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

Recommend

heyang   |   We have carefully selected several similar problems for you:  5085 5084 5081 5080 5079

题意:将二进制编码转化成字符命令,或将字符命令转化成二进制,共有六种命令,每种命令对应一个二进制编码,见题目表格;然后操作数有31种,分别是R1~R31,对应分别二进制00000~11111;其中要注意SET命令只有一个操作数,它的二进制编码最后5位全部写为0,若SET操作输入的二进制编码后五位不全是0,则输出Error!;不满足要求的命令也输出Error!,另外操作数不能是00000.

蛋疼的一题,考虑不周,比赛没做出来。。。。代码比较乱啊啊啊啊啊

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

char ope[6][5]={{"ADD"},{"SUB"},{"DIV"},{"MUL"},{"MOVE"},{"SET"}};
char code[6][7]={{"000001"},{"000010"},{"000011"},{"000100"},{"000101"},{"000110"}};
char co[31][6]={ {"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"}};
char s1[5],s2[10];

int OP(char str[])
{
    for (int i=0;i<6;i++)
    {
        if (strcmp(str,ope[i])==0)
            return i;
    }
}

int main()
{
    int n;
    while (~scanf("%d",&n))
    {
        if (n==1)
        {
            scanf("%s%s",s1,s2);
            int x=OP(s1);
            int dd=x;
            printf("%s",code[x]);
            x=s2[1]-'0';
            int ok=0;
            if (isdigit(s2[2]))
            {
                ok=1;
                x=x*10;
                x=x+s2[2]-'0';
            }
            x--;
            printf("%s",co[x]);
            if (dd==5)
            {
                printf("00000\n");
                continue;
            }
            if (ok)
            {
                x=s2[5]-'0';
                if (isdigit(s2[6]))
                {
                    x=x*10;
                    x=x+s2[6]-'0';
                }
            }
            else
            {
                x=s2[4]-'0';
                if (isdigit(s2[5]))
                {
                    x=x*10;
                    x=x+s2[5]-'0';
                }
            }
            x--;
            printf("%s\n",co[x]);
        }
        else if (n==0)
        {
            scanf("%s",s1);
            int len=strlen(s1);
            int t=1;
            int sum=0;
            for (int i=5;i>=0;i--)
            {
                int x=s1[i]-'0';
                sum+=t*x;
                t=t*2;
            }
            if (sum>6||sum==0)
            {
                printf("Error!\n");
                continue;
            }
            sum--;
            int o=sum;
            int flag=sum;
//            printf("%s",ope[sum]);
            t=1;
            sum=0;
            for (int i=10;i>=6;i--)
            {
                int x=s1[i]-'0';
                sum+=t*x;
                t=t*2;
            }
            if (sum>31||sum==0)
            {
                printf("Error!\n");
                continue;
            }
            int aa=sum;
//            printf(" R%d",sum);
            if (flag==5)
            {
                t=1;
                sum=0;
                for (int i=15;i>=11;i--)
                {
                    int x=s1[i]-'0';
    //                printf("x=%d\n",x);
                    sum+=t*x;
                    t=t*2;
                }
                if (sum==0)
                {
                    printf("%s",ope[o]);
                    printf(" R%d",aa);
                    printf("\n");
                    continue;
                }
                else
                {
                    printf("Error!\n");
                    continue;
                }
            }
            t=1;
            sum=0;
            for (int i=15;i>=11;i--)
            {
                int x=s1[i]-'0';
//                printf("x=%d\n",x);
                sum+=t*x;
                t=t*2;
            }
            if (sum>31||sum==0)
            {
                printf("Error!\n");
                continue;
            }
            printf("%s",ope[o]);
            printf(" R%d",aa);
            printf(",R%d\n",sum);
        }
    }
    return 0;
}

时间: 2024-10-14 06:47:20

Instruction (hdu 5083)的相关文章

Valentine&#39;s Day Round 1001.Ferries Wheel(hdu 5174)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5174 题目意思:给出 n 个人坐的缆车值,假设有 k 个缆车,缆车值 A[i] 需要满足:A[i−1]<A[i]<A[i+1](1<i<K).现在要求的是,有多少人满足,(他坐的缆车的值 + 他左边缆车的值) % INT_MAX == 他右边缆车的值. 首先好感谢出题者的样例三,否则真的会坑下不少人.即同一部缆车可以坐多个人.由于缆车的值是唯一的,所以可以通过排序先排出缆车的位置.求出

最短路 (HDU 2544)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28836    Accepted Submission(s): 12480 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

BestCoder Round #70 Jam&#39;s math problem(hdu 5615)

Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ax^2+bx+cax?2??+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m)pqx?2??+(qk+mp)x+km=(px+k)(qx+m). He could only solve the problem in which p,q,m,

RPG的错排 (HDU 2068)

RPG的错排 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6746    Accepted Submission(s): 2738 Problem Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R是

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

字典树 Trie (HDU 1671)

Problem Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers: 1. Emergency 911 2. Alice 97 625 999 3. Bob 91 12 54 26 In this

BestCoder Round #1 1002 项目管理 (HDU 4858)

项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 738    Accepted Submission(s): 260 Problem Description 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的!两个节点间可能有多条边,不过一条边的两端必然是不同的节点.每个节点都有一个能量值. 现在我

Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b2, ...,  bt.需要满足两个条件(1)b1≤b2≤…≤bt   (2)b2−b1≤b3−b2≤?≤bt−bt−1.求出最大的 t 为多少. 遗留大半年的题目呀呀呀呀~~~~!!!首先非常感谢乌冬子大半年前的指点迷津,呕心沥血想了大半天举出个反例,以便指出我算法的错误.为了纪念这个伟大的人物(

BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 280 Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the