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(2, 5 - 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(2, 10 - 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(2, 15 - 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;

}

时间: 2024-10-17 07:01:11

BestCoder Round#15 1002-Instruction的相关文章

HDU BestCoder Round #1 1002 项目管理

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

Manacher BestCoder Round #49 ($) 1002 Three Palindromes

题目传送门 1 /* 2 Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 3 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间,位运算 >>1 比 /2 速度快,用了程序跑快200ms左右,位运算大法好 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1

贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an的值最优,每次还要和an比较 4 注意:不能选取两个相同的数 5 反思:比赛时想到了%p和sort,lower_bound,但是还是没有想到这个贪心方法保证得出最大值,还是题目做的少啊:( 6 */ 7 #include <cstdio> 8 #include <algorithm>

HDU 5281 BestCoder Round #47 1002:Senior&#39;s Gun

Senior's Gun Accepts: 235 Submissions: 977 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 学姐姐是一个酷酷的枪手. 她常常会随身携带n把枪,每把枪有一个攻击力a[i]. 有一天她遇到了m只怪兽,每只怪兽有一个防御力b[j].现在她决定用手中的枪消灭这些怪兽. 学姐姐可以用第i把枪消灭第j只怪兽当且仅当b[j]≤a[i],同时她会获

矩阵快速幂---BestCoder Round#8 1002

当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n)   f(n-1)] = [f(n-1)    f(n-2)] [ 1 1 ]     [ 1 0 ] 设A = [ 1 1 ]  [ 1 0 ] [f(n)   f(n-1)] = [f(n-2)   f(n-3)]*A*A[f(n)   f(n-1)] = [f(2)   f(1)]*A^(n-2)矩阵满足结合律,所以先计算A^

二分图判定+点染色 BestCoder Round #48 ($) 1002 wyh2000 and pupil

题目传送门 1 /* 2 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 3 每一次二分图匹配时,将点多的集合加大最后第一个集合去 4 注意:n <= 1,no,两个集合都至少有一人:ans == n,扔一个给另一个集合 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 #in

暴力+降复杂度 BestCoder Round #39 1002 Mutiple

题目传送门 1 /* 2 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 3 若没有,默认加0,nlogn复杂度: 4 我用暴力竟然水过去了:) 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <algorithm> 11 using namespace std;

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个节点,用很多边连接起来,并且这个项目是连通的!两个节点间可能有多条边,不过一条边的两端必然是不同的节点.每个节点都有一个能量值. 现在我

BestCoder Round #4 1002

这题真是丧心病狂,引来今天的hack狂潮~ Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 10    Accepted Submission(s): 3 Problem Description There are N point on X-axis . Miaomiao would like t

BestCoder Round#15 1001-Love

http://acm.hdu.edu.cn/showproblem.php?pid=5082 Love Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 64    Accepted Submission(s): 51 Problem Description There is a Love country with many couples