标签:模拟
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 347 Accepted Submission(s): 101
1 ADD R1,R2 0 0000010000100010 0 1111111111111111
0000010000100010 ADD R1,R2 Error!
解题思路:
阅读理解模拟题,指令为“Set”时要单独且特别注意。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stdlib.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;
map<string,string>z;//操作符与二进制指令相对应
map<string,int>z2;//二进制数与十进制数相对应
void prepare()
{
z["000001"]="ADD";
z["000010"]="SUB";
z["000011"]="DIV";
z["000100"]="MUL";
z["000101"]="MOVE";
z["000110"]="SET";
z2["00001"]=1;z2["00010"]=2;
z2["00011"]=3;z2["00100"]=4;
z2["00101"]=5;z2["00110"]=6;
z2["00111"]=7;z2["01000"]=8;
z2["01001"]=9;z2["01010"]=10;
z2["01011"]=11;z2["01100"]=12;
z2["01101"]=13;z2["01110"]=14;
z2["01111"]=15;z2["10000"]=16;
z2["10001"]=17;z2["10010"]=18;
z2["10011"]=19;z2["10100"]=20;
z2["10101"]=21;z2["10110"]=22;
z2["10111"]=23;z2["11000"]=24;
z2["11001"]=25;z2["11010"]=26;
z2["11011"]=27;z2["11100"]=28;
z2["11101"]=29;z2["11110"]=30;
z2["11111"]=31;
}
string find(string op)//寻找操作符对应的二进制指令
{
map<string,string>::iterator i;
for(i=z.begin();i!=z.end();i++)
{
if(i->second==op)
return (i->first);
if(i->first==op)
return (i->second);
}
return "Error!";
}
string find2(int op)//寻找十进制对应的二进制
{
map<string,int>::iterator i;
for(i=z2.begin();i!=z2.end();i++)
{
if(i->second==op)
return (i->first);
}
return "Error!";
}
int find22(string op)//寻找二进制数对应的十进制
{
map<string,int>::iterator i;
for(i=z2.begin();i!=z2.end();i++)
{
if(i->first==op)
return (i->second);
}
return -1;
}
string op,source;//输入
string zhi;//输入
string wa="Error!";
int sigh;
int main()
{
prepare();
while(scanf("%d",&sigh)!=EOF)
{
if(sigh==1)
{
cin>>op>>source;
if(op=="SET")//单独判断
{
cout<<"000110";
int a=0;//提取出来十进制数
for(int i=1;i<source.length();i++)
a=a*10+source[i]-'0';
cout<<find2(a);
cout<<"00000"<<endl;
}
else
{
string ans=find(op);
//提取出来两个数字
int p;
int a1=0,a2=0;
bool deng=0;
for(p=0;p<source.length();p++)
{
if(source[p]==',')
deng=1;
if(p>0&&deng==0)
a1=a1*10+source[p]-'0';
if(deng==1)
break;
}
p=p+2;
for(;p<source.length();p++)
a2=a2*10+source[p]-'0';
cout<<ans;
cout<<find2(a1)<<find2(a2);
cout<<endl;
}
}
else
{
cin>>zhi;
string zhiling=zhi.substr(0,6);
string temp=find(zhiling);
if(temp==wa)
{
cout<<wa<<endl;
continue;
}
string a1=zhi.substr(6,5);
string a2=zhi.substr(11,5);
int f1=find22(a1);
int f2=find22(a2);
if(temp=="SET")
{
if(a2!="00000"||f1==-1)
{
cout<<wa<<endl;
continue;
}
cout<<temp<<" R"<<f1<<endl;
continue;
}
if(f1==-1||f2==-1)//判断两个数字是否合法
{
cout<<wa<<endl;
continue;
}
cout<<temp<<" ";
cout<<"R"<<f1<<",R"<<f2<<endl;
}
}
return 0;
}
[ACM] HDU 5083 Instruction (模拟)
标签:模拟
原文地址:http://blog.csdn.net/sr_19930829/article/details/40474553