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\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 }
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] == '\0') // R1 为个位数a2 = s2[1] - '0';elsea2 = (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] = '\0';printf("%s\n", ans);}else{scanf("%s", s1);a1 = a2 = a3 = 0;binary_to_ins(5, 0, a1);if (a1 > 6 || a1 == 0) // 找不到合适指令printf("Error!\n");else{binary_to_ins(15, 11, a3); // SET指令R2!=0if (a1 == 6 && a3 > 0)printf("Error!\n");else{binary_to_ins(10, 6, a2);if (a2 == 0 || a3 == 0 && a1 != 6) // 除SET其他指令R1!=0printf("Error!\n");else if (a1 == 6)printf("SET R%d\n", a2);elseprintf("%s R%d,R%d\n", instruct[a1], a2, a3);}}}}return 0; }
View Code
转载于:https://www.cnblogs.com/windysai/p/4053254.html
BestCoder15 1002.Instruction(hdu 5083) 解题报告相关推荐
- hdu 2058 解题报告 - The sum problem
hdu 2058 解题报告 - The sum problem 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2058 等差求和公式: Sn=(a1+aN ...
- BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...
- BestCoder4 1002 Miaomiao's Geometry (hdu 4932) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 题目意思:给出 n 个点你,需要找出最长的线段来覆盖所有的点.这个最长线段需要满足两个条件:(1 ...
- BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数 ...
- HDU 1506 解题报告 Largest Rectangle in a Histogram (单调栈)
看题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题意比较明显,就是找以某一个矩形为高的最大的矩形.这个题可以用单调栈来求解,需要注意的是如果从 ...
- Fibonacci Tree HDU - 4786——解题报告
立志用更少的代码做更高效的表达 Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do some ...
- 1002 图论专练 解题报告
T1 重量不同的硬币 有N(1 <= N <= 1,000)个硬币,编号为1..N. 给出W(1 <= W <= 3,000)个推断对(A,B),表示硬币A比硬币B重. 寻找 ...
- hdu 1754 解题报告 I Hate It
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 1870解题报告(愚人节的礼物)
愚人节的礼物 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class ...
最新文章
- JavaScript原生代码处理JSON的一些高频次方法合集
- 扩增子图表解读5火山图:差异OTU数量及变化规律
- 新冠病毒侵入人体全过程!从脚趾到大脑,科学家追踪到病毒对身体的巨大摧残...
- 职业相关职位及职位能力要求知识点大纲范围
- C#实现字符串按多个字符采用Split方法分割得到数组
- 关于$.getJson
- 游戏教玩家学java,技术|帮你学习Java语言的游戏
- Python排序 插入排序
- 两个简单的前台显示构架01
- Jconsole/jvisualvm远程监控weblogic中间件配置
- 数学建模-常见模型整理及分类
- CAS 服务端的搭建
- 捕捉百合网的女同志和echarts展示
- 关于路由器中设置IP与网关不在同一网段方法的问题
- PPT导出图片质量太差?简单操作直接导出印刷质地图片
- 打包小程序公众图标素材6113个菜单栏素材
- 单反相机镜头焦距与被摄物体的实际距
- 嵌入式系统测试平台——ETest
- @Inject 注解的使用
- Java基础学习(9)
热门文章
- 数据库,部分函数依赖,传递函数依赖,完全函数依赖,三种范式的区别
- mysql开发要注意什么_Mysql日常开发注意要点
- python配置环境是干啥的_Python配置环境
- a标签不可点击_如何在Notion中做多级标签?-Notion102
- ubuntu查看oracle客户端,ubuntu 9.04 下安装 oracle 客户端oracle-xe-client
- dell服务器启动顺序如何设置_如何即时设置一个静态文件服务器
- Java中Lambda表达式
- VS2008+Qt 项目目录
- 关于AI和区块链的技术落地,你不知道的是……
- spring session的生命周期