Little Rabbit’s Equation(给定等式判断几进制下实现的)出自--南昌理工学院ACM集训队

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

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 1digit, 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

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cctype>//下面有这个函数的解释及用法
using namespace std;

string raw,num1,num2,num3;
char o;
int minr;

viod init()
{
	num1.clear();
	num2.clear();
	num3.clear();
	minr=2;
}

void get_num()
{
	int now=1;
	for(char c:raw)
	{
	if(!isdigit(c)&&!isalpha(c))
	{
	now++;
	if(c!='=')
	o=c;
	}
	else
	{
	if(isdigit(c)) 
	minr=max(minr,c-'0'+1);
	else
	minr=max(minr,c-'A'+11);
	if(now==1)
	num1+=c;
	else if(now==2)
	num2+=c;
	else num3+=c;
	}
    }
}
long long cal(string s,int r)
{
    long long re=0;
    for (char c:s)
    {
        if (isdigit(c)) re=re*r+c-'0';
        else re=re*r+c-'A'+10;
    }
     return re;
}

bool check(int r)
{
    long long t1=cal(num1,r);
    long long t2=cal(num2,r);
    long long t3=cal(num3,r);
    if (o=='+') return t1+t2==t3;
    if (o=='-') return t1-t2==t3;
    if (o=='*') return t1*t2==t3;
    if (t2==0) return false;
    if (t1%t2) return false;
    return t1/t2==t3;
}

int get_ans()
{
    for (int i=minr;i<=16;i++)
    {
        if (check(i)) return i;
    }
    return -1;
}

int main()
{
	while(cin>>raw)
	{
	init();
	get_num();
	cout<<get_ans()<<'\n';
	}
	getchar();
	getchar();
	return 0;
}

**#include **是#include <ctype.h>
好多头文件都是这样的哇
比如:math.h(c语言的说法)
cmath(c++的说法)

以下为字符函数库中常用的函数:

函数名称 返回值

isalnum() 如果参数是字母数字,即字母或数字,该函数返回true

isalpha() 如果参数是字母,该函数返回真

isblank() 如果参数是空格或水平制表符,该函数返回true

iscntrl() 如果参数是控制字符,该函数返回true

isdigit() 如果参数是数字(0~9),该函数返回true

isgraph() 如果参数是除空格之外的打印字符,该函数返回true

islower() 如果参数是小写字母,该函数返回true

isprint() 如果参数是打印字符(包括空格),该函数返回true

ispunct() 如果参数是标点符号,该函数返回true

isspace() 如果参数是标准空白字符,如空格、进纸、换行符、回车
、水平制表符或者垂直制表符,该函数返回true

isupper() 如果参数是大写字母,该函数返回true

isxdigit() 如果参数是十六进制的数字,即0~9、af、AF,该函数返回true

tolower() 如果参数是大写字符,则返回其小写,否则返回该参数

toupper() 如果参数是小写字母,则返回其大写,否则返回该参数

常用的有:
tolower()——toupper()
isupper()——islower()
isalnum()——isalpha()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值