HDU 6828 Little Rabbit‘s Equation(2020杭电多校训练第六场)

Little Rabbit’s Equation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 593 Accepted Submission(s): 285

Problem Description
Little Rabbit is interested in radix. In a positional numeral system, the radix is the number of unique digits, including the digit 0, used to represent numbers. For example, for the decimal system (the most common system in use today) the radix is ten, because it uses the ten digits from 0 to 9. Generally, in a system with radix b (b>1), a string of digits d1…dn denotes the number d1bn−1+d2bn−2+⋯+dnb0, where 0≤di<b.

Little Rabbit casually writes down an equation. He wonders which radix this equation fits.

Input
The are several test cases. Each test case contains a string in a line, which represents the equation Little Rabbit writes down. The length of the string is at most 15. The input is terminated by the end-of-file.

The equation’s format: number, operator, number, =, number. There’s no whitespace in the string.

Each number has at least 1 digit, which may contain digital numbers 0 to 9 or uppercase letters A to F (which represent decimal 10 to 15). The number is guaranteed to be a non-negative integer, which means it doesn’t contain the radix point or negative sign. But the number may contain leading zeros.

The operator refers to one of +, −, ∗, or /. It is guaranteed that the number after / will not be equal to 0. Please note that the division here is not integer division, so 7/2=3 is not correct.

Output
For each test case, output an integer r (2≤r≤16) in a line, which means the equation is correct in the system with radix r. If there are multiple answers, output the minimum one. If there is no answer between 2 and 16, output −1.

Sample Input

1+1=10
18-9=9
AA*AA=70E4
7/2=3 

Sample Output

2
10
16
-1

题意:
给你一个算式,问你他是几进制的加减乘除,/不是整除,会有小数点。
题解:
因为数据比较小,那么就把 **( )符号( )=( )**三个括号中的内容分别提取出来,然后进行2-16进制的循环判断计算即可,除法要另外处理。
Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include <fstream>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1.0),eps=1e-8;
const ll mod =  998244353;
const int maxn=1e5+5;
vector<int>v1,v2,v3;
char cha;
ll add(ll x,ll y)
{
    if(cha=='+')
    {
        return x+y;
    }
    else if(cha=='-')
    {
        return x-y;
    }
    else if(cha=='*')
    {
        return x*y;
    }
}
int main()
{
    string s;
    while(cin>>s)
    {
        int f=0;
        v1.clear();
        v2.clear();
        v3.clear();
        int l=s.size();
        string tmp;
        int flag;
        for(int i=0; i<l; i++)
        {
            if(s[i]=='+'||s[i]=='/'||s[i]=='*'||s[i]=='-')
            {
                cha=s[i];
                flag=i;
                break;
            }
            tmp+=s[i];
        }
        for(int i=0; i<tmp.size(); i++)
        {
            if(tmp[i]>='0'&&tmp[i]<='9')
                v1.push_back(tmp[i]-'0');
            else
                v1.push_back(tmp[i]-'A'+10);
        }
        tmp.clear();
        for(int i=flag+1; i<l; i++)
        {
            if(s[i]=='=')
            {
                flag=i;
                break;
            }
            tmp+=s[i];
        }
        for(int i=0; i<tmp.size(); i++)
        {
            if(tmp[i]>='0'&&tmp[i]<='9')
                v2.push_back(tmp[i]-'0');
            else
                v2.push_back(tmp[i]-'A'+10);
        }
        tmp.clear();
        for(int i=flag+1; i<l; i++)
        {
            if(s[i]=='=')
            {
                flag=i;
                break;
            }
            tmp+=s[i];
        }
        for(int i=0; i<tmp.size(); i++)
        {
            if(tmp[i]>='0'&&tmp[i]<='9')
                v3.push_back(tmp[i]-'0');
            else
                v3.push_back(tmp[i]-'A'+10);
        }
        for(int i=2; i<=16; i++)
        {
            int ff=0;
            ll sum1=0,sum2=0,sum3=0;
            for(int j=0; j<v1.size(); j++)
            {
                if(v1[j]>=i)
                {
                    ff=1;
                    break;
                }
                sum1=sum1*i+v1[j];
            }
            if(ff)
                continue;
            for(int j=0; j<v2.size(); j++)
            {
                if(v2[j]>=i)
                {
                    ff=1;
                    break;
                }
                sum2=sum2*i+v2[j];
            }
            if(ff)
                continue;
            for(int j=0; j<v3.size(); j++)
            {
                if(v3[j]>=i)
                {
                    ff=1;
                    break;
                }
                sum3=sum3*i+v3[j];
            }
            if(ff)
                continue;
            // printf("%lld %lld %lld\n",sum1,sum2,sum3);
            if(cha!='/')
            {
                if(add(sum1,sum2)==sum3)
                {
                    f=1;
                    printf("%d\n",i);
                    break;
                }
            }
            else
            {
                if(sum2*sum3==sum1)
                {
                    f=1;
                    printf("%d\n",i);
                    break;
                }
            }
        }
        if(!f)
            printf("-1\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值