BestCoder Round #15 1002

来源:互联网 发布:程序员 简历怎么写 编辑:程序博客网 时间:2024/06/05 10:18

Instruction


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


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.
1a,b31
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
1ADD R1,R20000001000010001001111111111111111
 
Sample Output
0000010000100010ADD R1,R2Error!
 
Statistic | Submit | Clarifications | Back

题意:就是先输入一个p数字,如果p是1就是把英文操作转换成对应的数字,如果p是0,则是把数字转换成相对应的英文操作,如果不能转换就输出error。
做法:模拟题比较烦,错了四次才过,主要要注意是的error的情况,首先判断前6位,看是否有所对应的操作,如果没有直接输出error,如果有则分情况考虑,如果对应的是SET则判断a>0&&b==0,如果是其他则要判断a>0&&b>0。主要注意这些,其他都比较好处理。代码写的有点繁琐了。。。。懒得精简了。。。。。
#include <iostream>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>#include <algorithm>#include<ctime>#define esp 1e-6#define LL  long long#define inf 0x0f0f0f0fusing namespace std;int num[10];void solve(int x){    memset(num,0,sizeof(num));    int k=0;    int i;    while(x!=0)        {            num[k++]=x%2;            x=x/2;        }    for(i=4;i>=0;i--)        printf("%d",num[i]);}int main(){    int p;    char s[55];    int a,b,i;    char c,c1;    while(scanf("%d",&p)!=EOF)    {        if(p==1)        {            scanf("%s",s);            getchar();            if(strcmp(s,"SET")==0)            {                scanf("%c%d",&c,&a);                printf("000110");                solve(a);                printf("00000\n");            }            if(strcmp(s,"ADD")==0)            {                scanf("%c%d,%c%d",&c,&a,&c1,&b);                printf("000001");                solve(a);                solve(b);                printf("\n");            }            if(strcmp(s,"SUB")==0)            {                scanf("%c%d,%c%d",&c,&a,&c1,&b);                printf("000010");                solve(a);                solve(b);                printf("\n");            }            if(strcmp(s,"DIV")==0)            {                scanf("%c%d,%c%d",&c,&a,&c1,&b);                printf("000011");                solve(a);                solve(b);                printf("\n");            }            if(strcmp(s,"MUL")==0)            {                scanf("%c%d,%c%d",&c,&a,&c1,&b);                printf("000100");                solve(a);                solve(b);                printf("\n");            }            if(strcmp(s,"MOVE")==0)            {                scanf("%c%d,%c%d",&c,&a,&c1,&b);                printf("000101");                solve(a);                solve(b);                printf("\n");            }        }        int k1,k2;        if(p==0)        {            k1=k2=0;            char ss[50];            char s1[50],s2[50],s3[50];            char fh[10][100]={"000001","000010","000011","000100","000101","000110"};            scanf("%s",ss);            for(i=0;i<=5;i++)                s1[i]=ss[i];            for(i=6;i<=10;i++)                s2[k1++]=ss[i];            s2[k1]='\0';            for(i=11;i<=15;i++)                s3[k2++]=ss[i];            s3[k2]='\0';            int flag=-1;            for(i=0;i<=5;i++)                if(strcmp(fh[i],s1)==0)                    flag=i;            if(flag==-1)            {                printf("Error!\n");                continue;            }            int ss1,ss2;            ss1=ss2=0;           // printf("**%s %s\n",s1,s2);            for(i=0;i<5;i++)                ss1+=pow(2,4-i)*(s2[i]-'0');            for(i=0;i<5;i++)                ss2+=pow(2,4-i)*(s3[i]-'0');            if(flag==5)            {                if(ss2==0&&ss1!=0)                printf("SET R%d\n",ss1);                else                    printf("Error!\n");            }            if(flag==0)            {                if(ss1!=0&&ss2!=0)                printf("ADD R%d,R%d\n",ss1,ss2);                else                printf("Error!\n");            }            if(flag==1)            {                if(ss1!=0&&ss2!=0)                printf("SUB R%d,R%d\n",ss1,ss2);                else                    printf("Error!\n");            }            if(flag==2)            {                if(ss1!=0&&ss2!=0)                printf("DIV R%d,R%d\n",ss1,ss2);                else                    printf("Error!\n");            }            if(flag==3)            {                if(ss1!=0&&ss2!=0)                printf("MUL R%d,R%d\n",ss1,ss2);                else                    printf("Error!\n");            }            if(flag==4)            {                if(ss1!=0&&ss2!=0)                printf("MOVE R%d,R%d\n",ss1,ss2);                else                    printf("Error!\n");            }        }    }}


0 0
原创粉丝点击