题意:给出汇编指令,解释出编码或者给出编码,解释出汇编指令。

解法:简单模拟,按照给出的规则一步一步来就好了,主要是注意“SET”的情况,还有要输出的东西最好放到最后一起输出,中间如果一旦不对就可以及时跳出去。

其他也没什么了,只要细心点,多测几组样例就好了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define N 100007 string op[] = {"ADD","SUB","DIV","MUL","MOVE","SET"};
string bop[] = {"","","","","",""};
string bina[],itostr[]; void init()
{
for(int i=;i<;i++)
{
string now = "";
int tmp = i;
for(int j=;j<;j++)
{
if(tmp%) now += "";
else now += "";
tmp /= ;
}
reverse(now.begin(),now.end());
bina[i] = now; //i的二进制形式 tmp = i;
string no = "";
while(tmp)
{
no += tmp%+'';
tmp/=;
}
reverse(no.begin(),no.end());
itostr[i] = no; //i的十进制形式
}
} int main()
{
string num,A;
char ss[],R1,R2;
int sign,i,j,a,b;
init();
while(scanf("%d",&sign)!=EOF)
{
if(sign == )
{
cin>>A;
getchar();
if(A != "SET")
scanf("%c%d,%c%d",&R1,&a,&R2,&b);
else
scanf("%c%d",&R1,&a);
for(i=;i<;i++)
{
if(A == op[i])
break;
}
if(i == ) { puts("Error!"); continue; }
if(A != "SET")
{
if(R1 != 'R' || R2 != 'R' || a <= || a >= || b <= || b >=)
{
puts("Error!");
continue;
}
cout<<bop[i]<<bina[a]<<bina[b]<<endl;
}
else
{
if(R1 != 'R'|| a <= || a >= )
{
puts("Error!");
continue;
}
cout<<bop[i]<<bina[a]<<""<<endl;
}
}
else
{
cin>>num;
string A,B,C;
string oA,oB,oC;
A = num.substr(,);
B = num.substr(,);
C = num.substr(,);
for(i=;i<;i++)
{
if(A == bop[i])
break;
}
if(i == ) { puts("Error!"); continue; }
oA = op[i];
if(op[i] != "SET")
{
for(i=;i<;i++)
{
if(B == bina[i])
break;
}
if(i == ) { puts("Error!"); continue; }
oB = "R"+itostr[i];
for(i=;i<;i++)
{
if(C == bina[i])
break;
}
if(i == ) { puts("Error!"); continue; }
oC = "R"+itostr[i];
cout<<oA<<" "<<oB<<","<<oC<<endl;
}
else
{
if(C != "") { puts("Error!"); continue;}
for(i=;i<;i++)
{
if(B == bina[i])
break;
}
if(i == ) { puts("Error!"); continue; }
oB = "R"+itostr[i];
cout<<oA<<" "<<oB<<endl;
}
}
}
return ;
}
04-14 02:02