(进制转化)

A few days earlier a certain mathematician had been fired from his job so he has made up his mind to take revenge on his former employers and has changed all the numbers in their databases to their corresponding forms in different numerical systems using different bases. At the beginning it seemed to everyone to be just a stupid joke and hopefully they would soon find the correct data hidden somewhere.They were wrong, because even the backup database copies have been changed. The only hint, they were given was that all the data had been transformed to systems with such a base that it is the smallest base in which input numbers are squares.


Your task is to find these bases. You need to hurry up, because the whole firm’s activity depends on your database fix. You may however assume that:


• for each number, there exists a sought base and it is less than 100
• all the digits in input numbers are characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• each number written in decimal system is smaller that 1000000000


Input

Data set consists of lines containing single numbers. Occurrence of ‘0’ means the end of data set (0 is not treated as valid data).


Output
For each number you should find a smallest base of a numerical system in which this number is a square of some other number. Each number should be outputted in separate line.
Sample Input
61
1100
509
510
1013
0
Sample Output
8
3
12
16
6


给出一个数,问这个数在哪个进制下会是一个完全平方数,我们可以枚举它的进制后将他转化到10进制下判断他是否为完全平方数。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 55

char str[500];
int len;

ll solve(ll x) //假设其为x进制,并转化为10进制
{
    ll ans=str[0]-'0';
    for(int i=1;i<len;i++)
    {
        ans=ans*x+str[i]-'0';
    }
    return ans;
}

int main()
{
    int i,maxv;
    ll temp;
    while(scanf("%s",str)!=EOF)
    {
        len=strlen(str);
        if(len==1&&str[0]=='0') break;

        maxv=-1;
        for(i=0;i<len;i++)
        {
            maxv=max(maxv,str[i]-'0');
        }

        for(i=maxv+1;i<=100;i++) //这个数至少为maxv+1进制
        {
            temp=solve(i);
            ll t=(ll)sqrt(1.0*temp);
            if((t*t)==temp)
            {
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值