您的位置:首页 > 其它

1010. Radix (25)

2015-07-15 11:09 369 查看
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:

N1 N2 tag radix

Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag"
is 1, or of N2 if "tag" is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print "Impossible". If the solution is not unique, output the smallest possible radix.
Sample Input 1:
6 110 1 10

Sample Output 1:
2

Sample Input 2:
1 ab 1 2

Sample Output 2:
Impossible


提交代

——————————

刚刚开始的时候,自己想错了,还以为radix不会很大呢,因为只表示到0-35,还以为是只能到36进制呢。

代码就在下面了。

#include <iostream>
#include <cstring>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int conver(char n[], int r)
{
int res=0;
int i=0;
for(i=0;n[i]!='\0';i++)
{
if(n[i]<='9' && n[i]>='0')
{
res=res*r+(n[i]-'0');
}
else if(n[i]<='z' && n[i]>='a')
{
res=res*r+(n[i]-'a'+10);
}
}
return res;
}

int judge(char n[],int target)
{
int re=0;
for(int i=2; i<=36; i++)
{
re=conver(n,i);
if(re==target)
{
return i;
}
}
return -1;
}

int main(int argc, char** argv) {
int tag,radix;
char n1[11],n2[11];
int res1=0,res2=0,res=0;

scanf("%s %s %d %d",n1,n2,&tag,&radix);
if(tag==1)
{
res1=conver(n1,radix);//radix很大的呢
res=judge(n2,res1);
if(res == -1)
{
printf("Impossible\n");
}
else
{
printf("%d\n",res);
}
}
else if(tag==2)
{
res2=conver(n2,radix);
res=judge(n1,res2);
if(res == -1)
{
printf("Impossible\n");
}
else
{
printf("%d\n",res);
}
}
return 0;
}




果断是因为自己没有考虑到很大的radix的情况下。

然后换成long long int,并且采用二分查找的方式,在一位童靴的博客里面看到的。

#include <iostream>
#include <cstring>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

long long int conver(string n, long long int r)
{
long long int res=0;
long long int d=1;
for(int i=n.size()-1; i>=0; i--)
{
if(n[i]<='9' && n[i]>='0')
{
res=res+(n[i]-'0')*d;
}
else if(n[i]<='z' && n[i]>='a')
{
res=res+(n[i]-'a'+10)*d;
}
d*=r;
}
return res;
}
long long int findLeastRadix(string n)
{
int d=-1;
for(int i=0; i<n.size(); i++)
{
int res=0;
if(n[i]<='9' && n[i]>='0') res=n[i]-'0';
else res=n[i]-'a'+10;

if(res>d) d=res;
}
return d+1;
}
int cmp(string n, long long int r, long long int target)
{
long long int res=0,d=1;
for(int i=n.size()-1; i>=0; i--)
{
if(n[i]<='9' && n[i]>='0')
{
res=res+(n[i]-'0')*d;
}
else if(n[i]<='z' && n[i]>='a')
{
res=res+(n[i]-'a'+10)*d;
}
d*=r;
if(res>target) return 1;
}
if(res==target) return 0;
return -1;
}

long long int findRadix(string n,long long int low, long long int high, int target)// 二分查找
{
long long int re=0;
long long int mid=(low+high)/2;
int tg;
while(low<=high)
{
mid=(low+high)/2;
tg=cmp(n,mid,target);
if(tg==-1) low=mid+1;
else if(tg==0) return mid;
else high=mid-1;
}

return -1;
}

int main(int argc, char** argv) {
long long int radix;
int tag;
long long int res1=0,res2=0,res=0;
string n1,n2;

cin>>n1>>n2>>tag>>radix;
if(n1=="1" && n2 == "1")
{
cout<<"2"<<endl;
return 0;
}
if(n1==n2)
{
cout<<radix<<endl;
return 0;
}
if(tag==1)
{
res1=conver(n1,radix);//radix很大的呢
long long int low=findLeastRadix(n2);
long long int high=(res1+1>low+1)?res1+1:low+1;
res=findRadix(n2,low,high,res1);
}
else if(tag==2)
{
res2=conver(n2,radix);
long long int low=findLeastRadix(n1);
long long int high=(res2+1>low+1)?res2+1:low+1;
res=findRadix(n1,low,high,res2);
}

if(res == -1)
{
printf("Impossible\n");
}
else
{
printf("%lld\n",res);
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: