2020 杭电多校第六场 Little Rabbit‘s Equation(模拟,进制)

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

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

Source
2020 Multi-University Training Contest 6

题意:
给一个表达式,求这个表达式是几进制

思路:
直接枚举进制。注意最大数字决定最小进制。

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long ll;
const int maxn = 100 + 7;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

char s[maxn];

int get(char x) {
    if(x <= '9' && x >= '0') return x - '0';
    else if(x <= 'F' && x >= 'A') return x - 'A' + 10;
    return -1;
}

ll to_num(ll b,int l,int r) { //b进制
    ll ans = 0;
    for(int i = l;i <= r;i++) {
        ans = ans * b + get(s[i]);
    }
    return ans;
}

int main() {
    while(fgets(s,100,stdin)) {
        int pos1 = 0,pos2 = 0,pos3 = 0;
        int num = 2;//最小进制
        for(int i = 0;i < 100;i++) {
            if(s[i] == '+' || s[i] == '*' || s[i] == '-' || s[i] == '/') {
                pos1 = i;
            } else if(s[i] == '=') {
                pos2 = i;
            } else if(get(s[i]) != -1){
                int now = get(s[i]);
                num = max(num,now + 1);
            } else {
                pos3 = i;
                break;
            }
        }
        
        int ans = -1;
        for(int i = num;i <= 16;i++) {
            ll A = to_num(i, 0, pos1 - 1);
            ll B = to_num(i, pos1 + 1, pos2 - 1);
            ll C = to_num(i, pos2 + 1, pos3 - 1);
            if(s[pos1] == '+') {
                if(A + B == C) {
                    ans = i;break;
                }
            } else if(s[pos1] == '-') {
                if(A - B == C) {
                    ans = i;break;
                }
            } else if(s[pos1] == '*') {
                if((__int128)A * B == (__int128)C) {
                    ans = i;break;
                }
            } else if(s[pos1] == '/') {
                if((__int128)B * C == (__int128)A) {
                    ans = i;break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(#,其他比赛题目,#,模拟)