PAT-1010. Radix (25)
题目描述: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
分析:
(1)一开始以为对进制进行遍历就行,但是没有考虑到起始radix可以是非常大的数。
最初的代码如下:
[cpp] view plaincopy
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
#define max 10
int change(char *s, int base)
{
int temp = 0;
int i;
for(i=0; i<strlen(s); i++)
{
if(s[i]>='0' && s[i]<='9')
{
if((s[i] - '0') >= base) return -1;
temp = temp*base + (s[i] - '0');
}
else
{
if( (s[i]-'a'+10) >= base ) return -1;
temp = temp*base + (s[i] - 'a' + 10);
}
}
return temp;
}
int main( )
{
char N1[max];
char N2[max];
int tag;
long long result1,result2,radix;
long long i;
bool flag = false;
scanf("%s%s%d%ld",N1,N2,&tag,&radix);
if(tag == 1)
{
result1 = change(N1,radix);
for(i=2; i<=10000000; i++)
{
result2 = change(N2,i);
if(result2 == result1) {flag = true; break;}
}
}
else if(tag == 2)
{
result2 = change(N2,radix);
for(i=2; i<=10000000; i++)
{
result1 = change(N1,i);
if(result1 == result2) {flag = true; break;}
}
}
if(flag)
cout<<i<<endl;
else
cout<<"Impossible"<<endl;
return 0;
}
运行后结果有两组“答案错误”,估计是radix不够大的缘故。
#include <stdio.h>
#include <string.h>
#define max 11
char a[4][max];
long long num2Dec(char * p, long long radix)
{
long long i;
long long len=strlen(p);
long long digit = 0;
long long m = 1;
long long sum = 0;
for(i=len-1;i>=0;i--)
{
if(p[i]>='a'&&p[i]<='z')
digit= p[i] - 'a' + 10;
else if(p[i]>='0'&& p[i]<='9')
digit=p[i] - '0';
sum+=digit*m;
m*=radix;
}
return sum;
}
int findLeastRadix(char *p)
{
long long len=strlen(p);
long long low=0;
long long num;
long long i;
for(i=len-1;i>=0;i--)
{
if(p[i]>='a'&&p[i]<='z')
num= p[i] - 'a' + 10;
else if(p[i]>='0'&& p[i]<='9')
num=p[i] - '0';
if(num+1>low)
low=num+1;
}
return low;
}
int compare(char* p, long long radix, long long target)
{
long long i;
long long len=strlen(p);
long long m = 1;
long long num = 1;
long long sum = 0;
for(i=len-1;i>=0;i--)
{
if(p[i]>='a'&&p[i]<='z')
num= p[i] - 'a' + 10;
else if(p[i]>='0'&& p[i]<='9')
num=p[i] - '0';
sum+=num*m;
m*=radix;
if(sum>target) //avoid overflow
return 1;
}
if(sum>target)
return 1;
else if(sum<target)
return -1;
else
return 0;
}
long long binarySearch(char* p, long long low, long long high, long long target){
long long mid = low;
int tag;
while(low<=high) {
tag = compare(p, mid, target);
switch(tag) {
case -1:
low = mid + 1;
break;
case 0:
return mid;
case 1:
high = mid - 1;
}
mid = (low + high) >> 1;
}
return -1;
}
int main(int argc, char* argv[])
{
int i;
int tag;
int base;
long long n1;
long long n2;
long long low;
long long high;
long long radix;
for(i=0; i<4; i++) {
scanf("%s", a[i]);
}
tag = atoi(a[2]);
base = atoi(a[3]);
switch(tag) {
case 1 :
n1 = num2Dec(a[0], base);
low=findLeastRadix(a[1]);
high = (n1+1 > low+1) ? n1+1 : low+1;
radix = binarySearch(a[1], low, high, n1);
break;
case 2 :
n2 = num2Dec(a[1], base);
low=findLeastRadix(a[0]);
high补充:软件开发 , C++ ,
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊