B - Little Rabbit's Equation HDU - 6828
https://vjudge.net/contest/387998#problem/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
B - Little Rabbit's Equation HDU - 6828的更多相关文章
- (二分搜索)Can you solve this equation? -- hdu -- 2199
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2199 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- C - Can you solve this equation? HDU - 2199(二分水题)
Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100; ...
- Rabbit and Grass HDU - 1849 (Bash+Nim)
就是Bash 和 Nim 博弈的结合 可以直接 res ^= (Li + 1) % Mi 也可以 sg打个表 我打了个表 #include <iostream> #include &l ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- hdu 2199 Can you solve this equation?(高精度二分)
http://acm.hdu.edu.cn/howproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 MS ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1222 Wolf and Rabbit(gcd)
HDU 1222 Wolf and Rabbit (最大公约数)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- hdu 4778 Rabbit Kingdom(减少国家)
题目链接:hdu 4778 Rabbit Kingdom 题目大意:Alice和Bob玩游戏,有一个炉子.能够将S个同样颜色的宝石换成一个魔法石.如今有B个包,每一个包里有若干个宝石,给出宝石的颜色. ...
- HDU 4777 Rabbit Kingdom(树状数组)
HDU 4777 Rabbit Kingdom 题目链接 题意:给定一些序列.每次询问一个区间,求出这个区间和其它数字都互质的数的个数 #include <cstdio> #include ...
- HDU 2199 Can you solve this equation?(二分精度)
HDU 2199 Can you solve this equation? Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == ...
随机推荐
- Spring boot 项目集成Redisson抛异常 NoClassDefFoundError: Lorg/nustaq/serialization/FSTConfiguration
Spring boot 项目集成Redisson做分布式锁的时候,抛异常 NoClassDefFoundError: Lorg/nustaq/serialization/FSTConfiguratio ...
- 创建Spring Boot项目时,提示 Cannot download 'https://start.spring.io'
问题提出 在使用IDEA创建Spring Boot项目时,提示无法连接https://start.spring.io,内容如下: Cannot download 'https://start.spri ...
- Spring注解之@Autowired组件装配
前言 说起Spring的@Autowired注解,想必大家已经耳熟能详:对于小编而言,虽然一直知道怎么用,但是并没有去了解过,因此,本文就梳理一下@Autowired注解的功能,如有写的不准确的地方, ...
- java-mybatis-手动分页工具类
使用 List<PhcpSaBankAl> saBankAlList = phcpSaBankAlDao.getBankInfoList(loginUserType.getCityCode ...
- SAP采购订单屏幕控制
这里看交货计划的屏幕控制:交货计划增强,不可编辑 采购订单的程序是用类+屏幕写的. 所以直接看采购订单的程序:MEGUI 找到我们需要增强的屏幕,屏幕号1320 对应的类对象. BUILD_DYNPR ...
- hot100之栈
有效的括号(020) 跳过 最小栈(155) class MinStack { private final Deque<int[]> stack = new ArrayDeque<& ...
- 几分钟了解下java虚拟机--01
JDK, JRE, JVM的关系 ⚙ 解释器: 逐行转换字节码为机器码 即时编译器(JIT):将热点代码(经常执行的代码段)编译成高效的本地机器码,并缓存起来以供后续直接执行 Just-In-Time ...
- 使用systemd 监控服务并实现故障自动重启
一.为什么需要自动重启? 在生产环境中,服务可能因内存溢出.资源竞争.外部依赖中断等问题意外崩溃.手动恢复效率低下,而 systemd 的自动重启机制可在秒级内恢复服务,显著提升系统可用性. ⚙️ 二 ...
- 实现对C语言类学生管理系统文件存储的两种方法
学习javascript的时候曾经想做一个留言板的应用,但是却由于不知道如何存储失败了,由于做这个留言板的思路类似于C语言的学生管理系统,故此这次经历让我重新审视自己去学懂C语言的文件操作. 我重新用 ...
- CF2064F We Be Summing 题解
CF2064F We Be Summing 计数问题通常需要对计数对象找到一个独一无二的特征进行计数,否则只能进行容斥.注意到一个子区间中 \(i\) 从左到右的过程中,前一部分 \(\min\) 单 ...