2020 Multi-University Training Contest 6---- HDU--6828、Little Rabbit‘s Equation(模拟)

题目链接

题面:
在这里插入图片描述

题意:
判定给定等式在 2 − 16 2-16 216 进制,最低几进制下成立。

题解:
模拟即可,注意进制最低为2。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<bitset>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<ctime>
#define ui unsigned int
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define lc (cnt<<1)
#define rc (cnt<<1|1)
#define len(x)  (t[(x)].r-t[(x)].l+1)
#define tmid ((l+r)>>1)
#define fhead(x) for(int i=head[(x)];i;i=nt[i])
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;

const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e18;
const double alpha=0.75;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1.0);
const int hp=13331;
const int maxn=1100;
const int maxm=100100;
const int maxp=100100;
const int up=1100;

map<char,int>mp;
void init(void)
{
    mp['0']=0,mp['1']=1,mp['2']=2;
    mp['3']=3,mp['4']=4,mp['5']=5;
    mp['6']=6,mp['7']=7,mp['8']=8;
    mp['9']=9,mp['A']=10,mp['B']=11;
    mp['C']=12,mp['D']=13,mp['E']=14;
    mp['F']=15;
}

char str[110];

ll get(int l,int r,int p)
{
    ll ans=0;
    for(int i=l;i<=r;i++)
        ans=ans*p+mp[str[i]];
    return ans;
}

int main(void)
{
    init();
    while(scanf("%s",str+1)!=EOF)
    {
        int len=strlen(str+1);
        int pos1=0,pos2=0;
        int l=2,r=16;
        for(int i=1;i<=len;i++)
        {
            if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
                pos1=i;
            else if(str[i]=='=')
                pos2=i;
            else l=max(l,mp[str[i]]+1);
        }

        ll ans1,ans2,ans3;
        bool flag=false;
        for(int i=l;i<=r;i++)
        {
            ans1=get(1,pos1-1,i);
            ans2=get(pos1+1,pos2-1,i);
            ans3=get(pos2+1,len,i);
            if(str[pos1]=='+')
            {
                if(ans1+ans2==ans3)
                {
                    printf("%d\n",i);
                    flag=true;
                    break;
                }
            }
            else if(str[pos1]=='-')
            {
                if(ans1-ans2==ans3)
                {
                    printf("%d\n",i);
                    flag=true;
                    break;
                }
            }
            else if(str[pos1]=='*')
            {
                if(ans1*ans2==ans3)
                {
                    printf("%d\n",i);
                    flag=true;
                    break;
                }
            }
            if(str[pos1]=='/')
            {
                if(ans1/ans2==ans3&&ans1%ans2==0)
                {
                    printf("%d\n",i);
                    flag=true;
                    break;
                }
            }
        }
        if(flag==false) printf("-1\n");
    }
    return 0;
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页