学习大神们开始写题解。也有助于自己的思路整理
废话:今天第一状态算是不错,第一题没有像上次那样由于物理不好WA10+发还不对。。。。但是这道难度不大的模拟自己还是注意不好细节。。。。比赛时间没做出来,关键是对Error的处理。
思路:简单粗暴的模拟,注意关键是对Error的处理。
程序如下:细节地方已用注释标记
传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=5083
官方题解:http://http://bestcoder.hdu.edu.cn/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
string ADD="000001";
string SUB="000010";
string DIV="000011";
string MUL="000100";
string MOVE="000101";
string SET="000110";
string num[40]={"","00001","00010","00011","00100",
"00101","00110","00111","01000",
"01001","01010","01011","01100",
"01101","01110","01111","10000",
"10001","10010","10011","10100",
"10101","10110","10111","11000",
"11001","11010","11011","11100",
"11101","11110","11111"};
string s;
int dischange(int i,int j)//由已知数据为5位二进制,则可以输入i,j作为起止位置求其十进制
{
int num=0,k;
for(k=i;k<=j;k++)
num=num*2+s[k]-48;//注意字符与数字的区别
return num;
}
void no()
{
cout<<"Error!\n";
}
int main()
{
//freopen("input.txt","r",stdin);
int type;
while(cin>>type)
{
cin>>s;
if (type==1)
{
int flag=0;
if (s=="ADD") cout<<ADD;
else if (s=="SUB") cout<<SUB;
else if (s=="DIV") cout<<DIV;
else if (s=="MUL") cout<<MUL;
else if (s=="MOVE") cout<<MOVE;
else if (s=="SET") flag=1;
if (flag==0)
{
int number;
getchar();//处理空格
getchar();//处理逗号
scanf("%d",&number);
cout<<num[number];
getchar();
getchar();
scanf("%d",&number);
cout<<num[number];
printf("\n");
}
else
{
cout<<SET;
int number;
getchar();
getchar();
scanf("%d",&number);
cout<<num[number];
printf("00000\n");//处理b数字
}
}
else
{
int flag=0;
string s0;
s0=s.substr(0,6);//自己的基础不扎实,函数不熟悉
int num1=dischange(6,10);
int num2=dischange(11,15);
if (s0=="000001")
if (num1&&num2) printf("ADD R%d,R%d\n",num1,num2);
else no();
else if (s0=="000010")
if (num1&&num2) printf("SUB R%d,R%d\n",num1,num2);
else no();
else if (s0=="000011")
if (num1&&num2) printf("DIV R%d,R%d\n",num1,num2);
else no();
else if (s0=="000100")
if (num1&&num2) printf("MUL R%d,R%d\n",num1,num2);
else no();
else if (s0=="000101")
if (num1&&num2) printf("MOVE R%d,R%d\n",num1,num2);
else no();
else if (s0=="000110")
if (num1&&!num2) printf("SET R%d\n",num1);
else no();
else no();//所有的其他的情况都是不符合题意的
}
}
return 0;
}