zoukankan      html  css  js  c++  java
  • BestCoder15 1002.Instruction(hdu 5083) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083

    题目意思:如果给出 instruction 就需要输出对应的 16-bit binary code,给出16-bit binary code 就需要输出对应的instruction。

        由于不会截取的技巧,代码量非常可观 = =,所以说,一直很讨厌做模拟题!!!

        留下这代码,纪念一个代码还是不够精简的自己!!!内存和时间还能接受,也比较容易理解,不过好多重复代码= =。以下这个代码可以忽略,改到晕= =。之后会补上简单容易理解版滴......

        一...场.......噩..........梦!!!

        78Ms    284K   5708B

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #include <cstring>
      5 using namespace std;
      6 
      7 const int maxn = 20 + 5;
      8 const int N = 10 + 2;
      9 char instruct[7][N] = {"0", "ADD", "SUB", "DIV", "MUL", "MOVE", "SET"};
     10 char num_instruct[7][N] = {"0", "000001", "000010", "000011", "000100", "000101", "000110"};
     11 
     12 char R[32][N] = {"0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8",
     13                 "R9", "R10", "R11", "R12", "R13", "R14", "R15", "R16",
     14                 "R17", "R18", "R19", "R20", "R21", "R22", "R23", "R24",
     15                 "R25", "R26", "R27", "R28", "R29", "R30", "R31"};
     16 char num_R[32][N] = {"0", "00001", "00010", "00011", "00100", "00101", "00110",
     17                     "00111", "01000", "01001", "01010", "01011", "01100", "01101",
     18                     "01110", "01111", "10000", "10001", "10010", "10011", "10100",
     19                     "10101", "10110", "10111", "11000", "11001", "11010", "11011",
     20                     "11100", "11101", "11110", "11111"};
     21 
     22 char s1[N], s2[maxn];
     23 char s[maxn];
     24 
     25 int main()
     26 {
     27     #ifndef ONLINE_JUDGE
     28         freopen("ljy.txt", "r", stdin);
     29     #endif
     30 
     31     int ask;
     32     while (scanf("%d", &ask) != EOF)
     33     {
     34         char tmp1[N], tmp2[N];
     35         if (ask == 1)
     36         {
     37             int i;
     38             scanf("%s%s", s1, s2);
     39             for (i = 1; i < 7; i++)
     40             {
     41                 if (!strcmp(s1, instruct[i]))
     42                 {
     43                     printf("%s", num_instruct[i]);
     44                     break;
     45                 }
     46             }
     47             if (!strcmp(s1, "SET"))     // SET指令单独处理
     48             {
     49                 for (i = 0; i < 32; i++)
     50                 {
     51                     if (!strcmp(s2, R[i]))
     52                     {
     53                         printf("%s00000
    ", num_R[i]);
     54                         break;
     55                     }
     56                 }
     57             }
     58             else
     59             {
     60                 int l1 = 0;
     61                 int len = strlen(s2);
     62                 for (i = 0; i < len; i++)
     63                 {
     64                     if (s2[i] == ',')
     65                         break;
     66                     tmp1[l1++] = s2[i];
     67                 }
     68                 tmp1[l1] = '';      // 截取Rdestination
     69                 int r = i+1;
     70                 for (i = 0; i < 32; i++)
     71                 {
     72                     if (!strcmp(tmp1, R[i]))
     73                     {
     74                         printf("%s", num_R[i]);
     75                         break;
     76                     }
     77                 }
     78                 int l2 = 0;
     79                 for (i = r; i < len; i++)
     80                     tmp2[l2++] = s2[i];
     81                 tmp2[l2] = '';     // 截取Rsource
     82                 for (int i = 1; i < 32; i++)
     83                 {
     84                     if (!strcmp(tmp2, R[i]))
     85                     {
     86                         printf("%s
    ", num_R[i]);
     87                         break;
     88                     }
     89                 }
     90             }
     91         }
     92         else
     93         {
     94             scanf("%s", s);
     95             char ans[1][maxn];
     96             int len1 = strlen(s);
     97             int l1 = 0;
     98             for (int i = 0; i < 6; i++)
     99                 tmp1[l1++] = s[i];
    100             tmp1[l1] = '';
    101             bool flag = false;
    102             for (int i = 1; i < 7; i++)
    103             {
    104                 if (!strcmp(tmp1, num_instruct[i]))
    105                 {
    106                     strcpy(ans[0], instruct[i]);   // 有可能是SET指令,这要继续往后看判断
    107                     flag = true;
    108                     break;
    109                 }
    110              }
    111             if (!flag)     // 找不到指令匹配
    112                 printf("Error!
    ");
    113             else
    114             {
    115                 int l1 = 0;
    116                 for (int i = 6; i < 11; i++)
    117                     tmp1[l1++] = s[i];
    118                 tmp1[l1] = '';    // Rdestination
    119                 
    120                 int l2 = 0;
    121                 for (int i = 11; i < 16; i++)
    122                     tmp2[l2++] = s[i];
    123                 tmp2[l2] = '';     // Rsource
    124 
    125                 if (!strcmp(ans[0], "SET"))    
    126                 {
    127                     if (!strcmp(tmp2, "00000") && strcmp(tmp1, "00000"))  // 符合条件的形式:000110?????00000
    128                     {
    129                         for (int i = 1; i < 32; i++)
    130                         {
    131                             if (!strcmp(tmp1, num_R[i]))
    132                             {
    133                                 printf("SET %s
    ", R[i]);
    134                                 break;
    135                             }
    136                         }
    137                     }
    138                     else
    139                         printf("Error!
    ");
    140                 }
    141 
    142                 else
    143                 {
    144                     if (!strcmp(tmp2, "00000") || !strcmp(tmp1, "00000"))   // 不合法的Registers
    145                         printf("Error!
    ");
    146                     else
    147                     {
    148                         printf("%s ", ans[0]);    // 除SET之外的其他指令
    149                         for (int i = 1; i < 32; i++)
    150                         {
    151                             if (!strcmp(tmp1, num_R[i]))
    152                             {
    153                                 printf("%s,", R[i]);
    154                                 break;
    155                             }
    156                         }
    157                         for (int i = 1; i < 32; i++)
    158                         {
    159                             if (!strcmp(tmp2, num_R[i]))
    160                             {
    161                                 printf("%s
    ", R[i]);
    162                                 break;
    163                             }
    164                         }
    165                     }
    166                 }
    167             }
    168         }
    169     }
    170     return 0;
    171 }
    View Code

       简化版: 62Ms   292K  2787B

      

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    const int N = 10 + 2;
    char instruct[7][N] = {"0", "ADD", "SUB", "DIV", "MUL", "MOVE", "SET"};
    
    char s1[2*N], s2[2*N];
    char ans[2*N];
    
    inline void ins_to_binary(int end, int st, int a)
    {
        for (int i = end; i >= st; i--)
        {
            ans[i] = a % 2 + '0';
            a >>= 1;
        }
    }
    
    inline void binary_to_ins(int end, int st, int &a)
    {
        int k = 1;
        for (int i = end; i >= st; i--)
        {
            a += (s1[i] - '0') * k;
            k <<= 1;
        }
    }
    
    int main()
    {
        int ask, a1, a2, a3;
        while (scanf("%d", &ask) != EOF)
        {
            if (ask == 1)
            {
                scanf("%s%s", s1, s2);
                for (int i = 1; i <= 6; i++)
                {
                    if (strcmp(s1, instruct[i]) == 0)
                    {
                        a1 = i;
                        break;
                    }
                }
                if (a1 == 6)   // SET 指令
                    a3 = 0;
                else
                {
                    int k = 1;
                    a3 = 0;
                    for (int i = strlen(s2)-1; i > 0; i--)  // R2
                    {
                        if (s2[i] == 'R')
                            break;
                        else
                        {
                            a3 += (s2[i] - '0') * k;
                            k *= 10;
                        }
                    }
                }
                if (s2[2] == ',' || s2[2] == '')   // R1 为个位数
                    a2 = s2[1] - '0';
                else
                    a2 = (s2[1]-'0') * 10 + s2[2]-'0';   // R1 为十位数
    
                ins_to_binary(15, 11, a3);
                ins_to_binary(10, 6, a2);
                ins_to_binary(5, 0, a1);
                ans[16] = '';
                printf("%s
    ", ans);
            }
            else
            {
                scanf("%s", s1);
                a1 = a2 = a3 = 0;
                binary_to_ins(5, 0, a1);
                if (a1 > 6 || a1 == 0)   // 找不到合适指令
                    printf("Error!
    ");
                else
                {
                    binary_to_ins(15, 11, a3);   // SET指令R2!=0
                    if (a1 == 6 && a3 > 0)
                        printf("Error!
    ");
                    else
                    {
                        binary_to_ins(10, 6, a2);
                        if (a2 == 0 || a3 == 0 && a1 != 6)     // 除SET其他指令R1!=0
                            printf("Error!
    ");
                        else if (a1 == 6)
                            printf("SET R%d
    ", a2);
                        else
                            printf("%s R%d,R%d
    ", instruct[a1], a2, a3);
                    }
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树
    POJ1050 To the Max 最大子矩阵
    POJ1259 The Picnic 最大空凸包问题 DP
    POJ 3734 Blocks 矩阵递推
    POJ2686 Traveling by Stagecoach 状态压缩DP
    iOS上架ipa上传问题那些事
    深入浅出iOS事件机制
    iOS如何跳到系统设置里的各种设置界面
    坑爹的私有API
    业务层网络请求封装
  • 原文地址:https://www.cnblogs.com/windysai/p/4053254.html
Copyright © 2011-2022 走看看