HDU 5083/BC 15B Instruction

19 篇文章 0 订阅

题意:两种串让你互相转换,前六位对应一个字符串,中间五位和后五位分别对应一个二进制数值 

题解:没什么难度,只是有一点不符合的就算非法,有两个要考虑的细节,首先SET的时候后面R2是为0的 其次注意b、c的范围

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#define mem(x,y) memset(x,y,sizeof(x))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,double> pii;
#define bug puts("===========");
#define zjc puts("");
const double pi=(acos(-1.0));
const double eps=1e-8;
const ll INF=1e18+10;
const ll inf=1e9+10;
const int mod=1e9+7;
const int maxn=100000+10;
const int maxm=1e6+5;

map<string,string> MP;
map<string,string>mp;
char aa[1005];
int la;
bool in(int x){
    return x>=1&&x<=31;
}
void OT(int a){
    char ans[10];
    ans[5]='\0';
    for(int i=4; i>=0; i--){
        ans[i]=a%2+'0';
        a/=2;
    }
    printf("%s",ans);
}
void go(int n){
    string a;
    int i,b=0,c=0;
    for(i=0; aa[i]!=' '&&i<la; i++) a+=aa[i];
    if(n!=1||aa[i]!=' '||aa[i+1]!='R'||MP.count(a)==0){
        puts("Error!");
        return;
    }
    for(i=i+2; i<la&&aa[i]<='9'&&aa[i]>='0'; i++) b=b*10+aa[i]-'0';
    if(a=="SET"){
        if(!in(b)||i!=la){
            puts("Error!");
            return;
        }
        printf("000110"); OT(b);OT(0);puts("");
        return;
    }
    if(aa[i]!=','||aa[i+1]!='R'){
        puts("Error!");
        return;
    }
    for(i=i+2; i<la&&aa[i]<='9'&&aa[i]>='0'; i++) c=c*10+aa[i]-'0';
    if(!in(b)||!in(c)||i!=la){
        puts("Error!");
        return;
    }
    cout<<MP[a]; OT(b);OT(c); puts("");
    return;
}
void initmp(){
    mp["000001"]="ADD";
    mp["000010"]="SUB";
    mp["000011"]="DIV";
    mp["000100"]="MUL";
    mp["000101"]="MOVE";
    mp["000110"]="SET";
    MP["ADD"]= "000001";
    MP["SUB"]= "000010";
    MP["DIV"]= "000011";
    MP["MUL"]= "000100";
    MP["MOVE"]="000101";
    MP["SET"]= "000110";
}
void go2(){
    string a;
    for(int i=0; i<6; i++) a+=aa[i];
    if(la!=16||mp.count(a)==0){
        puts("Error!");
        return;
    }
    int b=0,c=0;
    for(int i=6;i<11;i++) b=b*2+aa[i]-'0';
    for(int i=11;i<16;i++) c=c*2+aa[i]-'0';
    if(!in(b)){
        puts("Error!");
        return;
    }
    if(a=="000110"){
        if(c!=0) puts("Error!");
        else cout<<mp[a]<<" R"<<b<<endl;
        return;
    }
    if(!in(c)){
        puts("Error!");
        return;
    }
    cout<<mp[a]<<" R"<<b<<",R"<<c<<endl;
}
int main()
{
    initmp();
    int n;
    while(~scanf("%d",&n)){
        getchar();
        gets(aa);
        la=strlen(aa);
        if(n) go(n);
        else go2();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值