首页 > 代码库 > BestCoder Round#15 1002-Instruction

BestCoder Round#15 1002-Instruction

http://acm.hdu.edu.cn/showproblem.php?pid=5083

 


官方题解——》

1002 Instruction先考虑编码,首先找到operation对应的编码,如果是SET就找后面的一个R后面跟着的数字a,令b=0,否则找后面第一个R后面的数字当作a,第二个R后面的数字当作b,最后依次输出operation二进制编码,a, b的二进制编码。再说解码,先将前6位,中间5位和后面5位转化成十进制记为oid, a, b。如果oid<1||oid>6就是Error!,如果oid<6那么a,b都不能为0,如果oid==6那么a!=0&&b==0。其它情况都是Error!,最后按照oid,a,b输出指令即可。

 下面来代码。。水平有限,比较丑,勿怪-_-|||

#include <stdio.h>
#include <string.h>
#include <math.h>

int num[32][5] = {
    0,0,0,0,0,
    0,0,0,0,1,
    0,0,0,1,0,
    0,0,0,1,1,
    0,0,1,0,0,
    0,0,1,0,1,
    0,0,1,1,0,
    0,0,1,1,1,
    0,1,0,0,0,
    0,1,0,0,1,
    0,1,0,1,0,
    0,1,0,1,1,
    0,1,1,0,0,
    0,1,1,0,1,
    0,1,1,1,0,
    0,1,1,1,1,
    1,0,0,0,0,
    1,0,0,0,1,
    1,0,0,1,0,
    1,0,0,1,1,
    1,0,1,0,0,
    1,0,1,0,1,
    1,0,1,1,0,
    1,0,1,1,1,
    1,1,0,0,0,
    1,1,0,0,1,
    1,1,0,1,0,
    1,1,0,1,1,
    1,1,1,0,0,
    1,1,1,0,1,
    1,1,1,1,0,
    1,1,1,1,1
};

int op[10][6] = {
    0,0,0,0,0,0,
    0,0,0,0,0,1,
    0,0,0,0,1,0,
    0,0,0,0,1,1,
    0,0,0,1,0,0,
    0,0,0,1,0,1,
    0,0,0,1,1,0
};

char op2[7][6] = {
    "",
    "ADD",
    "SUB",
    "DIV",
    "MUL",
    "MOVE",
    "SET"
};

int main(){
    int type, a, b, i, j, c;
    char str[20];
    while(scanf("%d", &type) != EOF){
        if(type == 1){
            scanf("%s ", str);
            if(strcmp(str, "ADD") == 0){
                scanf("%*c%d,%*c%d", &a, &b);
                for(i = 0; i < 6; i++){
                    printf("%d", op[1][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[a][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[b][i]);
                }
                printf("\n");
            }
            else if(strcmp(str, "SUB") == 0){
                scanf("%*c%d,%*c%d", &a, &b);
                for(i = 0; i < 6; i++){
                    printf("%d", op[2][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[a][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[b][i]);
                }
                printf("\n");
            }
            else if(strcmp(str, "DIV") == 0){
                scanf("%*c%d,%*c%d", &a, &b);
                for(i = 0; i < 6; i++){
                    printf("%d", op[3][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[a][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[b][i]);
                }
                printf("\n");
            }
            else if(strcmp(str, "MUL") == 0){
                scanf("%*c%d,%*c%d", &a, &b);
                for(i = 0; i < 6; i++){
                    printf("%d", op[4][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[a][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[b][i]);
                }
                printf("\n");
            }
            else if(strcmp(str, "MOVE") == 0){
                scanf("%*c%d,%*c%d", &a, &b);
                for(i = 0; i < 6; i++){
                    printf("%d", op[5][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[a][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[b][i]);
                }
                printf("\n");
            }
            else if(strcmp(str, "SET") == 0){
                scanf("%*c%d", &a);
                for(i = 0; i < 6; i++){
                    printf("%d", op[6][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d", num[a][i]);
                }
                for(i = 0; i < 5; i++){
                    printf("%d"0);
                }
                printf("\n");
            }
        }
        else{
            scanf("%s", str);
            for(c = i = 0; i < 6; i++){
                if(str[i] == 1){
                    c += (int)pow(25 - i);
                }
            }
            if(c > 6 || c == 0){
                printf("Error!\n");
                continue;
            }
            for(a = 0, i = 6; i < 11; i++){
                if(str[i] == 1){
                    a += (int)pow(210 - i);
                }
            }
            if(a > 32 || a == 0){
                printf("Error!\n");
                continue;
            }
            for(b = 0, i = 11; i < 16; i++){
                if(str[i] == 1){
                    b += (int)pow(215 - i);
                }
            }
            if(b > 32){
                printf("Error!\n");
                continue;
            }
            if(c == 6 && b != 0){
                printf("Error!\n");
                continue;
            }
            if(c == 6 && a != 0 && b == 0){
                printf("%s R%d\n", op2[c], a);
                continue;
            }
            if(c > 0 && c < 6 && a != 0 && b != 0){
                printf("%s R%d,R%d\n", op2[c], a, b);
                continue;
            }
            printf("Error!\n");
        }
    }
    return 0;

} 

BestCoder Round#15 1002-Instruction