题目:有一个数字串(字符0-9),求它的最小的100内的基底,使得这个数字串是一个平方数。
分析:数论,搜索,二分。枚举基底判断数字是否是平方数,找到最小的基底即可。
1.题目中的每个数字代表一个位:509(12)= 5*12^2 + 9 = 729 = 27^2;
2.利用long long类型防止数据溢出;
3.判断是否是平方式利用二分,找到开方后的整数部分,再平方判断即可;
说明:要考试了,(⊙v⊙)嗯;
#include <stdio.h>
#include <stdlib.h>
int get_max_bite(int n)
{
int ans = 0;
while (n) {
if (ans < n%10) {
ans = n%10;
}
n /= 10;
}
return ans;
}
long long get_decimal(int n, int base)
{
long long ans = 0LL;
int buf[9], cnt = 0;
while (n) {
buf[cnt ++] = n%10;
n /= 10;
}
while (cnt) {
ans *= base;
ans += buf[-- cnt];
}
return ans;
}
int bneray_search(long long key)
{
long long L = 1LL;
long long R = 1000000000LL;
long long M;
while (L < R) {
M = (L+R+1)/2; // 防止死循环
if (M*M > key) {
R = M-1;
}else {
L = M;
}
}
return L*L == key;
}
int main()
{
int n;
while (~scanf("%d",&n) && n) {
for (int i = get_max_bite(n)+1; i < 100; ++ i) {
if (bneray_search(get_decimal(n, i))) {
printf("%d\n",i);
break;
}
}
}
return 0;
}