1010. Radix (25)
2018-01-06 15:23
489 查看
1010. Radix (25)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
这题是真恶心,第一次刷提交了20几次,遍历,24分。第二次刷参看了算法笔记,采用了二分法,并且还要考虑上界和溢出。
第一次刷写的很烂。。。
#include<stdio.h>//24分
#include<string.h>
int main(){
char s1[11],s2[11];
int tag,radix,i,temp,flag=0,jinz; //进制
double sum1=0,sum2=0;
scanf("%s %s %d %d",s1,s2,&tag,&radix);
int len1=strlen(s1),len2=strlen(s2);
if(tag==1){ //计算第一个数的 十进制
// int max=s1[0];
for(i=0;i<len1;i++){
// if(s1[i]>max){
// max=s1[i];
// }
if(s1[i]>='0'&&s1[i]<='9'){
temp=s1[i]-'0';
}
else{
temp=s1[i]-'a'+10;
}
sum1=sum1*radix+temp;
}
int max=s2[0];
for(jinz=2;jinz<=9999999;jinz++){ //对第二个数 遍历 进制
sum2=0;
for(i=0;i<len2;i++){
if(s2[i]>max){
max=s2[i];
}
if(s2[i]>='0'&&s2[i]<='9'){
temp=s2[i]-'0';
}
else{
temp=s2[i]-'a'+10;
}
sum2=sum2*jinz+temp;
}
if(sum1==sum2){
if(max>='0'&&max<='9'&&jinz>max-'0'){//还要满足进制数 比每一位都要大!
flag=1;break; //找到进制
}
if(max>='a'&&max<='z'&&jinz>max-'a'+10){
flag=1;break; //找到进制
}
}
}
if(flag==1){
printf("%d",jinz);
}
else{
printf("Impossible");
}
}
if(tag==2){
// int max=s2[0];
for(i=0;i<len2;i++){
// if(s2[i]>max){
// max=s2[i];
// }
if(s2[i]>='0'&&s2[i]<='9'){
temp=s2[i]-'0';
}
else{
temp=s2[i]-'a'+10;
}
sum2=sum2*radix+temp;
}
int max=s1[0];
for(jinz=2;jinz<=9999999;jinz++){
sum1=0;
for(i=0;i<len1;i++){
if(s1[i]>max){//
max=s1[i];
}
if(s1[i]>='0'&&s1[i]<='9'){
temp=s1[i]-'0';
}
else{
temp=s1[i]-'a'+10;
}
sum1=sum1*jinz+temp;
}
if(sum1==sum2){
if(max>='0'&&max<='9'&&jinz>max-'0'){ //还要满足进制数 比每一位都要大!
flag=1;break; //找到进制
}
if(max>='a'&&max<='z'&&jinz>max-'a'+10){
flag=1;break; //找到进制
}
}
}
if(flag==1){
printf("%d",jinz);
}
else{
printf("Impossible");
}
}
}
第二次还是有很多重复代码。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
char s1[15],s2[15];
LL tag,radix;
LL sum;
LL fun(){
LL len,i;
if(tag==1){
len=strlen(s1);
for(i=0;i<len;i++){
if(s1[i]>='a'){
sum=sum*radix+(s1[i]-87);//a:97
}
else{
sum=sum*radix+s1[i]-'0';
}
}
}
else{
len=strlen(s2);
for(i=0;i<len;i++){
if(s2[i]>='a'){
sum=sum*radix+(s2[i]-87);//a:97
}
else{
sum=sum*radix+s2[i]-'0';
}
}
}
return sum;
}
LL findminradix(){
LL minradix='0';
LL len,i;
if(tag==1){
len=strlen(s2);
for(i=0;i<len;i++){
if(s2[i]>=minradix){
minradix=s2[i];
}
}
}
else{
len=strlen(s1);
for(i=0;i<len;i++){
if(s1[i]>=minradix){
minradix=s1[i];
}
}
}
if(minradix>=97){
minradix=minradix-87;
}
else{
minradix=minradix-'0';
}
return minradix+1;
}
LL fun2(LL mid){
LL tempsum=0;
int i;
if(tag==1){
for(i=0;s2[i];i++){
if(s2[i]>='a'){
tempsum=tempsum*mid+(s2[i]-87);//a:97
}
else{
tempsum=tempsum*mid+s2[i]-'0';
}
}
}
else{
for(i=0;i<s1[i];i++){
if(s1[i]>='a'){
tempsum=tempsum*mid+(s1[i]-87);//a:97
}
else{
tempsum=tempsum*mid+s1[i]-'0';
}
}
}
return tempsum;
}
LL binary(LL minradix){
LL left=minradix,right=max(minradix,sum)+1;//上界 max(minradix,sum)+1
while(left<=right){
LL mid=(left+right)/2;
LL tempsum=fun2(mid);
if(tempsum==sum){
return mid;
}
else if(tempsum<0){//考虑溢出。。。
right=mid-1;
}
else if(tempsum<sum){
left=mid+1;
}
else{
right=mid-1;
}
}
return -1;
}
int main(){
int i;
scanf("%s %s %d %d",s1,s2,&tag,&radix);
sum=fun();
int minradix=findminradix();
int ans=binary(minradix);
if(ans!=-1){
printf("%d",ans);
}
else{
printf("Impossible");
}
}
相关文章推荐
- 【PAT甲级】1010. Radix (25)
- PAT(甲级)1010. Radix (25)
- pat 1010. Radix (25)
- PAT 1010. Radix (25)
- 1010. Radix (25)
- [PAT甲级]1010. Radix (25)(求另一个数的基数)
- 1010. Radix (25)-PAT甲级真题(二分法)
- PAT 1010. Radix (25)
- PAT 1010. Radix (25)
- 1010. Radix (25)(java版)
- 1010. Radix (25)
- 1010.Radix (25)...to be continued...
- PAT 1010 Radix (25)没有通过
- 1010. Radix (25)
- 1010. Radix (25)
- PAT (Advanced Level) Practise 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)