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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
const int N = + ;
char instruct[][N] = {"", "ADD", "SUB", "DIV", "MUL", "MOVE", "SET"};
char num_instruct[][N] = {"", "", "", "", "", "", ""}; char R[][N] = {"", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8",
"R9", "R10", "R11", "R12", "R13", "R14", "R15", "R16",
"R17", "R18", "R19", "R20", "R21", "R22", "R23", "R24",
"R25", "R26", "R27", "R28", "R29", "R30", "R31"};
char num_R[][N] = {"", "", "", "", "", "", "",
"", "", "", "", "", "", "",
"", "", "", "", "", "", "",
"", "", "", "", "", "", "",
"", "", "", ""}; char s1[N], s2[maxn];
char s[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("ljy.txt", "r", stdin);
#endif int ask;
while (scanf("%d", &ask) != EOF)
{
char tmp1[N], tmp2[N];
if (ask == )
{
int i;
scanf("%s%s", s1, s2);
for (i = ; i < ; i++)
{
if (!strcmp(s1, instruct[i]))
{
printf("%s", num_instruct[i]);
break;
}
}
if (!strcmp(s1, "SET")) // SET指令单独处理
{
for (i = ; i < ; i++)
{
if (!strcmp(s2, R[i]))
{
printf("%s00000\n", num_R[i]);
break;
}
}
}
else
{
int l1 = ;
int len = strlen(s2);
for (i = ; i < len; i++)
{
if (s2[i] == ',')
break;
tmp1[l1++] = s2[i];
}
tmp1[l1] = '\0'; // 截取Rdestination
int r = i+;
for (i = ; i < ; i++)
{
if (!strcmp(tmp1, R[i]))
{
printf("%s", num_R[i]);
break;
}
}
int l2 = ;
for (i = r; i < len; i++)
tmp2[l2++] = s2[i];
tmp2[l2] = '\0'; // 截取Rsource
for (int i = ; i < ; i++)
{
if (!strcmp(tmp2, R[i]))
{
printf("%s\n", num_R[i]);
break;
}
}
}
}
else
{
scanf("%s", s);
char ans[][maxn];
int len1 = strlen(s);
int l1 = ;
for (int i = ; i < ; i++)
tmp1[l1++] = s[i];
tmp1[l1] = '\0';
bool flag = false;
for (int i = ; i < ; i++)
{
if (!strcmp(tmp1, num_instruct[i]))
{
strcpy(ans[], instruct[i]); // 有可能是SET指令,这要继续往后看判断
flag = true;
break;
}
}
if (!flag) // 找不到指令匹配
printf("Error!\n");
else
{
int l1 = ;
for (int i = ; i < ; i++)
tmp1[l1++] = s[i];
tmp1[l1] = '\0'; // Rdestination int l2 = ;
for (int i = ; i < ; i++)
tmp2[l2++] = s[i];
tmp2[l2] = '\0'; // Rsource if (!strcmp(ans[], "SET"))
{
if (!strcmp(tmp2, "") && strcmp(tmp1, "")) // 符合条件的形式:000110?????00000
{
for (int i = ; i < ; i++)
{
if (!strcmp(tmp1, num_R[i]))
{
printf("SET %s\n", R[i]);
break;
}
}
}
else
printf("Error!\n");
} else
{
if (!strcmp(tmp2, "") || !strcmp(tmp1, "")) // 不合法的Registers
printf("Error!\n");
else
{
printf("%s ", ans[]); // 除SET之外的其他指令
for (int i = ; i < ; i++)
{
if (!strcmp(tmp1, num_R[i]))
{
printf("%s,", R[i]);
break;
}
}
for (int i = ; i < ; i++)
{
if (!strcmp(tmp2, num_R[i]))
{
printf("%s\n", R[i]);
break;
}
}
}
}
}
}
}
return ;
}
简化版: 62Ms 292K 2787B
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int N = + ;
char instruct[][N] = {"", "ADD", "SUB", "DIV", "MUL", "MOVE", "SET"}; char s1[*N], s2[*N];
char ans[*N]; inline void ins_to_binary(int end, int st, int a)
{
for (int i = end; i >= st; i--)
{
ans[i] = a % + '';
a >>= ;
}
} inline void binary_to_ins(int end, int st, int &a)
{
int k = ;
for (int i = end; i >= st; i--)
{
a += (s1[i] - '') * k;
k <<= ;
}
} int main()
{
int ask, a1, a2, a3;
while (scanf("%d", &ask) != EOF)
{
if (ask == )
{
scanf("%s%s", s1, s2);
for (int i = ; i <= ; i++)
{
if (strcmp(s1, instruct[i]) == )
{
a1 = i;
break;
}
}
if (a1 == ) // SET 指令
a3 = ;
else
{
int k = ;
a3 = ;
for (int i = strlen(s2)-; i > ; i--) // R2
{
if (s2[i] == 'R')
break;
else
{
a3 += (s2[i] - '') * k;
k *= ;
}
}
}
if (s2[] == ',' || s2[] == '\0') // R1 为个位数
a2 = s2[] - '';
else
a2 = (s2[]-'') * + s2[]-''; // R1 为十位数 ins_to_binary(, , a3);
ins_to_binary(, , a2);
ins_to_binary(, , a1);
ans[] = '\0';
printf("%s\n", ans);
}
else
{
scanf("%s", s1);
a1 = a2 = a3 = ;
binary_to_ins(, , a1);
if (a1 > || a1 == ) // 找不到合适指令
printf("Error!\n");
else
{
binary_to_ins(, , a3); // SET指令R2!=0
if (a1 == && a3 > )
printf("Error!\n");
else
{
binary_to_ins(, , a2);
if (a2 == || a3 == && a1 != ) // 除SET其他指令R1!=0
printf("Error!\n");
else if (a1 == )
printf("SET R%d\n", a2);
else
printf("%s R%d,R%d\n", instruct[a1], a2, a3);
}
}
}
}
return ;
}
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 都有 ...
- BestCoder20 1002.lines (hdu 5124) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...
- 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,使得 ...
- BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...
- 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,问是否在每个数 ...
- BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...
- BestCoder12 1002.Help him(hdu 5059) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...
- BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公 ...
- BestCoder8 1002 Revenge of Nim(hdu 4994) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4994 题目意思:有 n 个 heap(假设从左至右编号为1-n),每个 heap 上有一些 objec ...
随机推荐
- 45.Android 第三方开源库收集整理(转)
原文地址:http://blog.csdn.net/caoyouxing/article/details/42418591 Android开源库 自己一直很喜欢Android开发,就如博客签名一样, ...
- Jquery EasyUI的添加,修改,删除,查询等基本操作介绍
http://www.jb51.net/article/42016.htm 初识Jquery EasyUI看了一些博主用其开发出来的项目,页面很炫,感觉功能挺强大,效果也挺不错,最近一直想系统学习一套 ...
- vi显示行号
vi显示行号 :set nu 带行号查看,并不改变文件内容:set nonu 取消带行号查看在每个用户的主目录下,都有一个 vi 的配置文件".vimrc&quo ...
- 让Windows 7内置Administrator 用户也能使用指纹登录
前言 这周末重装了个系统,之前用windows 8 现在8.1预览版出来了,琢磨着是不是给升级玩玩.装上后感觉变化不大,后来一折腾,就换回windows 7 了(64位旗舰版).将安装时创建的用户删除 ...
- memcache内存分配机制
memcached的内存分配没有用到c语言中自带的malloc函数,因为这个函数分配内存的时候效率很低,对于这种要求快速响应,对效率要求非常高的缓存软件来说非常不合适. memcached用的是自己的 ...
- shell的执行顺序问题
&&,||,(),{},& 五个符号的运用 shell脚本执行命令的时候,有时候会依赖于前一个命令是否执行成功.而&&和||就是用来判断前一个命令执行效果的. ...
- MySql 创建只读账号
GRANT Select ON *.* TO reader@192.168.1.123 IDENTIFIED BY "123456" GRANT 可以立刻生效 在mysql 5 ...
- ubuntu系统 用户进入后命令行只有一个“$” 美元符号
在新添加用户后,切换到该用户下面后: 发现命令行前面只有一个$符号,很不方便. 虽然每次输入一个bash可以解决,但是太麻烦. 如何解决呢? sudo vi /etc/passwd 找到该用户 wan ...
- C# Web开发打开下载对话框代码
一个按钮的事件中写: string filename = Sever.UrlEncode("词库.txt"); Response.AddHeader("Content-D ...
- PHP程序员,因该养成 7 个面向对象的好习惯
在 PHP 编程早期,PHP 代码在本质上是限于面向过程的.过程代码 的特征在于使用过程构建应用程序块.过程通过允许过程之间的调用提供某种程度的重用. 但是,没有面向对象的语言构造,程序员仍然可以把 ...