HDU6828 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 denotes the number , where 0≤<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
大意就是判断给出的表达式在哪种进制下成立。
直接把字符串截成三段,从小到大枚举每种进制即可。
转换函数:
int Atoi(string s,int radix)
{
int ans=0;
for(int i=0;i<s.size();i++)
{
char t=s[i];
if(t>='0'&&t<='9')
{
if(t - '0' >= radix) return -1;
ans = ans * radix + 1ll * (t-'0');
}
else
{
if(1ll * (t - 'A' + 10) >= radix) return -1;
ans = ans * radix + 1ll * (t - 'A' + 10);
}
}
return ans;
}
注意的地方:
- 开long long
- 如果有一位数比当前枚举到的进制还大,直接跳过这种情况。
#include <bits/stdc++.h>
#define int long long
using namespace std;
int Atoi(string s,int radix)
{
int ans=0;
for(int i=0;i<s.size();i++)
{
char t=s[i];
if(t>='0'&&t<='9')
{
if(t - '0' >= radix) return -1;
ans = ans * radix + 1ll * (t-'0');
}
else
{
if(1ll * (t - 'A' + 10) >= radix) return -1;
ans = ans * radix + 1ll * (t - 'A' + 10);
}
}
return ans;
}
bool calc(int a, int b, int c, char opt)
{
if(opt == '+')
{
return a + b == c;
}
else if(opt == '-')
{
return a - b == c;
}
else if(opt == '*')
{
return a * b == c;
}
else
{
if(b == 0) return -1;
return a % b == 0 && a == b * c;
}
}
signed main()
{
string s;
while(cin >> s)
{
int opt = -1, eq = -1;
for(int i = 0; i < s.size(); i++)
{
if(s[i] >= '0' && s[i] <= '9' || s[i] >= 'A' && s[i] <= 'Z') continue;
if(opt == -1) opt = i;
else eq = i;
}
string a = s.substr(0, opt);
string b = s.substr(opt + 1, eq - opt - 1);
string c = s.substr(eq + 1);
bool flag = 0;
for(int i = 2; i <= 16; i++)
{
int aa = Atoi(a, i), bb = Atoi(b, i), cc= Atoi(c, i);
//cout << aa << ' '<<bb<<' '<<cc<<endl;
if(aa == -1 || bb == -1 || cc == -1) continue;
if(calc(aa, bb, cc, s[opt]))
{
cout << i << endl;
flag = 1;
break;
}
}
if(!flag)
{
cout << -1 << endl;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!