(进制转化)

来源:互联网 发布:python exe反编译工具 编辑:程序博客网 时间:2024/06/01 20:31

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 55char 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;}


原创粉丝点击