BestCoder15 1002.Instruction(hdu 5083) 解题报告

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

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

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

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

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

  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\n", 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] = ‘\0‘;      // 截取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] = ‘\0‘;     // 截取Rsource
 82                 for (int i = 1; i < 32; i++)
 83                 {
 84                     if (!strcmp(tmp2, R[i]))
 85                     {
 86                         printf("%s\n", 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] = ‘\0‘;
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!\n");
113             else
114             {
115                 int l1 = 0;
116                 for (int i = 6; i < 11; i++)
117                     tmp1[l1++] = s[i];
118                 tmp1[l1] = ‘\0‘;    // Rdestination
119
120                 int l2 = 0;
121                 for (int i = 11; i < 16; i++)
122                     tmp2[l2++] = s[i];
123                 tmp2[l2] = ‘\0‘;     // 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\n", R[i]);
134                                 break;
135                             }
136                         }
137                     }
138                     else
139                         printf("Error!\n");
140                 }
141
142                 else
143                 {
144                     if (!strcmp(tmp2, "00000") || !strcmp(tmp1, "00000"))   // 不合法的Registers
145                         printf("Error!\n");
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\n", R[i]);
162                                 break;
163                             }
164                         }
165                     }
166                 }
167             }
168         }
169     }
170     return 0;
171 }

时间: 2024-08-06 07:49:00

BestCoder15 1002.Instruction(hdu 5083) 解题报告的相关文章

BestCoder17 1002.Select(hdu 5101) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有相应的人,每个人又有相应的IQ.现在要求从这些classes挑选两个人,满足IQ之和 > k,不过要满足这两个人不能来自同一个class的. 解题思路不难想出,直接所有人两两之和 > k - 同一个class 两两之和 > k  就是答案了. 不过很容易超时!!!! 用二分就可以过了.二分有

BestCoder20 1002.lines (hdu 5124) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段的左端点和右端点.设 A 表示最多线段覆盖的点(当然这个 A 可以有多个啦,但这个无关紧要).现在需要求的是 A 被多少条线段覆盖. 我是不会做啦.......一开始还看不懂题目= = 以下是按照题解做的, 题解中的最大区间和,实际上就是求最大连续子序列的和. 1 #include <iostrea

hdu 2112 HDU Today 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目意思:又是求最短路的,不过结合埋字符串来考查. 受之前1004 Let the Balloon Rise 学到用map 的解法做之后,有点蠢蠢欲动,当时见到要用字典树做有点吓坏了(之前看过下,非一般难理解,所以暂时放下了).于是,死就死吧,硬住头皮用map做,反反复复修改终于过了. 首先是memory limit exceeded,因为无读清题目意思,直接开10000 * 10000的数组

BestCoder18 1002.Math Problem(hdu 5105) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得这个函数 f(x)= |a∗x3+b∗x2+c∗x+d| 最大. 我一开始做的时候,很天真的认为数据量这么小,一个一个试,暴力搜就肯定能得到答案啦.但是一个很严重的问题是,x 没有说是实数还是整数,所以枚举根本不可行. 于是就联想到应该使用高中求一元三次方程的方法来做.我当时是直接从 f(l), f

BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是1 2(下标为2 3),而第一长递增子序列也是(下标为 1 3). 我一开始天真的以为,还是利用求最长递增子序列的算法啦,第二长不就是对dp 数组sort完后(从小到大)的dp[cnt-1] 啦,接着就呵呵啦----= = 题解说,要加多一个 dp 数组,以便对当前下标值为 i 的数 a[i] 为结

BestCoder12 1002.Help him(hdu 5059) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否是合法的整数,如果是的话问是否在[a, b] 范围内,是则输出 YES,否则输出 NO 合法的整数:(1)非负整数:只有数字,没有前导0 (2)负数:负号后面只能跟着数字,负号前没有任何字符 首先这条题感觉不是出得太好,不过都是硬着头皮学人家做啦.反正一些很变态的数据可能还是过不了,但却AC的. 模

BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数,中位数是m的子序列有多少个. 我的做法被discuss 中的测试数据一下子就否定了. 这个是别人的做法,暂时留下来,有些地方还没真正弄懂,应该是缺了部分的知识没有学到... 留着先: (1)http://blog.csdn.net/hcbbt/article/details/38377815 (2

BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数只使用一次的前提下,分成若干部分,每部分数的长度 >= 3且满足是等差数列.可以的话输出 Yes ,否则 No . 比赛的时候过了pretest,那个开心啊--然后跟 XX 兽讨论了之后,才发现自己理解错了题目= = 要用到深搜,问组合嘛--组合就是有可能是(1, 2, 3, 4).(1, 2, 3

BestCoder7 1002 Little Pony and Alohomora Part I(hdu 4986) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4986 题目意思:有 n 个box(从左到右编号依次为1~n),每个box里面有一个随机的钥匙,有可能这条钥匙恰好可以开到这个box,但大多数情况下是不能够的.问期望值是多少.(例如对于两个box,有可能装着1 2 或者 2 1的 key,如果是1 2,那么就需要用两次咒语,而对于2 1(打开其中一个box就可以得到要开到另一个box的钥匙)只需要用一次即可.期望值就是 1/2 * 2 + 1/2 *