题目链接: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 = + ;
  8. const int N = + ;
  9. char instruct[][N] = {"", "ADD", "SUB", "DIV", "MUL", "MOVE", "SET"};
  10. char num_instruct[][N] = {"", "", "", "", "", "", ""};
  11.  
  12. char R[][N] = {"", "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[][N] = {"", "", "", "", "", "", "",
  17. "", "", "", "", "", "", "",
  18. "", "", "", "", "", "", "",
  19. "", "", "", "", "", "", "",
  20. "", "", "", ""};
  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 == )
  36. {
  37. int i;
  38. scanf("%s%s", s1, s2);
  39. for (i = ; i < ; 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 = ; i < ; 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 = ;
  61. int len = strlen(s2);
  62. for (i = ; 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+;
  70. for (i = ; i < ; i++)
  71. {
  72. if (!strcmp(tmp1, R[i]))
  73. {
  74. printf("%s", num_R[i]);
  75. break;
  76. }
  77. }
  78. int l2 = ;
  79. for (i = r; i < len; i++)
  80. tmp2[l2++] = s2[i];
  81. tmp2[l2] = '\0'; // 截取Rsource
  82. for (int i = ; i < ; 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[][maxn];
  96. int len1 = strlen(s);
  97. int l1 = ;
  98. for (int i = ; i < ; i++)
  99. tmp1[l1++] = s[i];
  100. tmp1[l1] = '\0';
  101. bool flag = false;
  102. for (int i = ; i < ; i++)
  103. {
  104. if (!strcmp(tmp1, num_instruct[i]))
  105. {
  106. strcpy(ans[], instruct[i]); // 有可能是SET指令,这要继续往后看判断
  107. flag = true;
  108. break;
  109. }
  110. }
  111. if (!flag) // 找不到指令匹配
  112. printf("Error!\n");
  113. else
  114. {
  115. int l1 = ;
  116. for (int i = ; i < ; i++)
  117. tmp1[l1++] = s[i];
  118. tmp1[l1] = '\0'; // Rdestination
  119.  
  120. int l2 = ;
  121. for (int i = ; i < ; i++)
  122. tmp2[l2++] = s[i];
  123. tmp2[l2] = '\0'; // Rsource
  124.  
  125. if (!strcmp(ans[], "SET"))
  126. {
  127. if (!strcmp(tmp2, "") && strcmp(tmp1, "")) // 符合条件的形式:000110?????00000
  128. {
  129. for (int i = ; i < ; 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, "") || !strcmp(tmp1, "")) // 不合法的Registers
  145. printf("Error!\n");
  146. else
  147. {
  148. printf("%s ", ans[]); // 除SET之外的其他指令
  149. for (int i = ; i < ; i++)
  150. {
  151. if (!strcmp(tmp1, num_R[i]))
  152. {
  153. printf("%s,", R[i]);
  154. break;
  155. }
  156. }
  157. for (int i = ; i < ; 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 ;
  171. }

简化版: 62Ms   292K  2787B

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. const int N = + ;
  8. char instruct[][N] = {"", "ADD", "SUB", "DIV", "MUL", "MOVE", "SET"};
  9.  
  10. char s1[*N], s2[*N];
  11. char ans[*N];
  12.  
  13. inline void ins_to_binary(int end, int st, int a)
  14. {
  15. for (int i = end; i >= st; i--)
  16. {
  17. ans[i] = a % + '';
  18. a >>= ;
  19. }
  20. }
  21.  
  22. inline void binary_to_ins(int end, int st, int &a)
  23. {
  24. int k = ;
  25. for (int i = end; i >= st; i--)
  26. {
  27. a += (s1[i] - '') * k;
  28. k <<= ;
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. int ask, a1, a2, a3;
  35. while (scanf("%d", &ask) != EOF)
  36. {
  37. if (ask == )
  38. {
  39. scanf("%s%s", s1, s2);
  40. for (int i = ; i <= ; i++)
  41. {
  42. if (strcmp(s1, instruct[i]) == )
  43. {
  44. a1 = i;
  45. break;
  46. }
  47. }
  48. if (a1 == ) // SET 指令
  49. a3 = ;
  50. else
  51. {
  52. int k = ;
  53. a3 = ;
  54. for (int i = strlen(s2)-; i > ; i--) // R2
  55. {
  56. if (s2[i] == 'R')
  57. break;
  58. else
  59. {
  60. a3 += (s2[i] - '') * k;
  61. k *= ;
  62. }
  63. }
  64. }
  65. if (s2[] == ',' || s2[] == '\0') // R1 为个位数
  66. a2 = s2[] - '';
  67. else
  68. a2 = (s2[]-'') * + s2[]-''; // R1 为十位数
  69.  
  70. ins_to_binary(, , a3);
  71. ins_to_binary(, , a2);
  72. ins_to_binary(, , a1);
  73. ans[] = '\0';
  74. printf("%s\n", ans);
  75. }
  76. else
  77. {
  78. scanf("%s", s1);
  79. a1 = a2 = a3 = ;
  80. binary_to_ins(, , a1);
  81. if (a1 > || a1 == ) // 找不到合适指令
  82. printf("Error!\n");
  83. else
  84. {
  85. binary_to_ins(, , a3); // SET指令R2!=0
  86. if (a1 == && a3 > )
  87. printf("Error!\n");
  88. else
  89. {
  90. binary_to_ins(, , a2);
  91. if (a2 == || a3 == && a1 != ) // 除SET其他指令R1!=0
  92. printf("Error!\n");
  93. else if (a1 == )
  94. printf("SET R%d\n", a2);
  95. else
  96. printf("%s R%d,R%d\n", instruct[a1], a2, a3);
  97. }
  98. }
  99. }
  100. }
  101. return ;
  102. }

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...

  3. 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,使得 ...

  4. BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...

  5. 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,问是否在每个数 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...

  8. BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公 ...

  9. BestCoder8 1002 Revenge of Nim(hdu 4994) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4994 题目意思:有 n 个 heap(假设从左至右编号为1-n),每个 heap 上有一些 objec ...

随机推荐

  1. Yii2 radioList设置默认值

    可以在对应的Controller的action中设置 $model->type = 1; 在view中 <?php $form = ActiveForm::begin(); ?>   ...

  2. dict内部方法

    代码: #dict内部方法 vdic={'name':'kamil','age':23} print(dir(vdic)) vdic1 = vdic.copy()#copy(self):浅拷贝 pri ...

  3. BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)

    无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...

  4. poj 1006 中国剩余定理解同余方程

    其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...

  5. 从js的repeat方法谈js字符串与数组的扩展方法

    js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...

  6. Ubuntu学习总结-06 安装 Nginx

    Nginx是由俄罗斯人(zhan dou min zu)开发的一款高性能的http和反向代理服务器,也可以用来作为邮件代理.相比较于其他的服务器,具有占用内存少,稳定性高等优势. 一 Ubuntu源码 ...

  7. Enum类型 枚举内部值/名

    enum Days { Nothing=0, Mon=1, Stu=2 } static void Main(string[] args) { foreach (int item in Enum.Ge ...

  8. php网站验证码的生成

    <?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/p ...

  9. yum被锁Another app is currently holding the yum lock; waiting for it to exit...

    可能是系统自动升级正在运行,yum在锁定状态中. 可以通过强制关掉yum进程: #rm -f /var/run/yum.pid 然后就可以使用yum了.

  10. TFS2008解除独占式锁定文件命令(转载)

    使用场景:如果项目团队成员A对项目某个文件以独占式方式签出,恰好那天该成员A没有来上班而成员需要对此文件进入修改并check in,这时需要先把A对该文件的锁定解除.没有IDE可以使用,只能使用下面的 ...