B - Little Rabbit's Equation HDU - 6828

https://vjudge.net/contest/387998#problem/B

 

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 $d_1 \dots d_n$ denotes the number $d_1b^{n-1} + d_2b^{n-2} + \dots + d_nb^0$, where $0 \le d_i < b$.

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

InputThe 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.OutputFor each test case, output an integer $r$ ($2 \le r \le 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

题意:
  给一些算式 判断该算式成立的数的最小进制

  保证都是整除,不是的话输出-1,长度为15,进制为2~16

思路:

  模拟 分字符串

   字符串通过+, -, *把数字分割出来,产生3段

   从小到大枚举每种进制

代码:

  

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;

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;
}
signed main()
{
	string s;
	while(cin >> s)
	{
		int opt = -1;
		int 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 a = Atoi(A, i), b = Atoi(B, i), c= Atoi(C, i);
			if(a == -1 || b == -1 || c == -1)
			{
				continue;
			}
			bool S = 0;
			if(opt == '+')
			{
				S = a + b == c;
			}
			else if(s[opt] == '-')
			{
				S = a - b == c;
			}
			else if(s[opt] == '*')
			{
				S = a * b == c;
			}
			else 
			{
				if(b == 0) return -1;
				S = a % b == 0 && a == b * c;
			}
			if(S)
			{
				cout << i << endl;
				flag = 1;
				break;
			}
		}
		if(!flag)
		{
			cout << -1 << endl;
		}
	}
}

  

 参考:https://www.cnblogs.com/lipoicyclic/p/13448226.html

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
校园失物招领系统管理系统按照操作主体分为管理员和用户。管理员的功能包括字典管理、论坛管理、公告信息管理、失物招领管理、失物认领管理、寻物启示管理、寻物认领管理、用户管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 校园失物招领系统管理系统可以提高校园失物招领系统信息管理问题的解决效率,优化校园失物招领系统信息处理流程,保证校园失物招领系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 ,管理员权限操作的功能包括管理公告,管理校园失物招领系统信息,包括失物招领管理,培训管理,寻物启事管理,薪资管理等,可以管理公告。 失物招领管理界面,管理员在失物招领管理界面中可以对界面中显示,可以对失物招领信息的失物招领状态进行查看,可以添加新的失物招领信息等。寻物启事管理界面,管理员在寻物启事管理界面中查看寻物启事种类信息,寻物启事描述信息,新增寻物启事信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值