pat1010. Radix (25) BUG!!!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int c2int(char c){
	if('0'<=c&&c<='9')
		return c-'0';
	else
		return c-'a'+10;
}
long s2decimal(char str[], long radix){
	long digit, sum=0;
	// 必须判断每一位是否小于radix
	int i=0;
	for(;i<strlen(str);i++){
		digit=c2int(str[i]);
		// radix应该是从大到小排列下来
		//if(digit>=radix)return -1;
		sum=radix*sum+digit;
		// sum溢出
		if(sum<0)return -1;
	}
	return sum;
}
int main(void){
	char str1[15], str2[15], str[15];
	int tag;
	long radix;
	long sum=0, num;
	long low=2,high;

	scanf("%s%s%d%ld", str1,str2,&tag,&radix);
	if(tag==1){
		sum=s2decimal(str1, radix);
		strcpy(str, str2);
	}else if(tag==2){
		sum=s2decimal(str2, radix);
		strcpy(str, str1);
	}
	// 二分查找。理性思考得出判断:2<=radix<=sum+1
	// 输出最小进制
	// only one digit,there exists multiple situations
	if(strlen(str)==1&&c2int(str[0])==sum){
		printf("%d\n", c2int(str[0])+1);
		system("pause");
		return 0;
	}
	high=sum+1;
	while(low<=high){
		num=s2decimal(str, (low+high)/2);
		if(num==-1||num>sum)
			high=(low+high)/2-1;
		else if(num<sum)
			low=(low+high)/2+1;
		else if(num==sum){
			printf("%d\n", (low+high)/2);
			system("pause");
			return 0;
		}
	}
	printf("Impossible\n");
	system("pause");
	return 0;
}

bug测试用例:17 321 1 10

输出:2

本程序可以通过pat,但是radix<digit,pat判断有误!!!

应该在s2decimal方法中判断digit<radix,改进后的s2decimal方法:

long s2decimal(char str[], long radix){
	long digit, sum=0;
	// 必须判断每一位是否小于radix
	int i=0;
	for(;i<strlen(str);i++){
		digit=c2int(str[i]);
		// radix应该是从大到小排列下来
		if(digit>=radix)return -2;
		sum=radix*sum+digit;
		// sum溢出
		if(sum<0)return -1;
	}
	return sum;
}

如有错误,欢迎指正!

时间: 2024-08-19 13:24:01

pat1010. Radix (25) BUG!!!的相关文章

PAT 1010. Radix (25)

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 i

1010. Radix (25)——PAT (Advanced Level) Practise

题目信息: 1010. Radix (25) 时间限制 400 ms 内存限制 32000 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 b

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 rad

PAT1010. Radix

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 ra

A1010. 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 rad

pat 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 N?1?? and N?2??, your task is to find the radi

1010. Radix (25)

简直是哭着做出来的,起码提交了22遍.创纪录了... 首先不能像上次那样枚举进制数,那样输入987654321 10 1 10 的话十分钟都算不完,题中只有一个超时很照顾人了... 考虑近似计算,利用y=a*x^n+b 算出n,进制数如果有,也不会超过n,因此考虑由n向下寻找. 注意,进制数不能小于数中最大的数字. 如果将两个数字作为字符串输入,注意排除字符串前面的0. 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN,

A1010 Radix (25 分)

一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbegin(),这里it作用就是指针一样,装的是地址,了解函数begin()是返回字符串str的一个元素地址和函数end()是返回字符串最后一个元素还要往后一位,而rbegin()和rend(),加了r后刚刚反过来了,rbegin()指向str的最后一个元素,rend()指向第一个元素还要往前一位. 第二

【PAT-A】解题报告

当你无聊的时候...可以考一考PAT...什么的...嗯 ========================我是日常分割线=========================== 1010. Radix (25) 1 /* 2 date:2016-09-05 3 author:CheerM 4 title:Radix(25) 5 6 题目描述: 7 Given a pair of positive integers, for example, 6 and 110, can this equation