1010. Radix (25)

这题主要意思是给出一个任意进制的数,在给你一个数,看第二个数在哪种进制下能够与第一个数相等。

自己一开始的思路是将其化为整数,然后在把另一个数按一定进制化为10进制数,再比较。

最终第一次还是错了,而且代码有点混乱,都是用int。

借鉴了sunbaigui的代码思路后,发现了并解决了一些问题:

http://blog.csdn.net/sunbaigui/article/details/8672395

而不是longlong,这题也让我了解了long 和longlong 的区别。

以及缩小查找范围,而不是直接从1开始,到小的结束。

以及巧妙的利用二分查找来进行数字的筛选,我一开始直接线性查找。。。

代码如下:

#include"stdio.h"
#include"string.h"
#include"math.h"
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
#define INF 0x7FFFFFFF

// convert any radix number to decimal,for the use of comparing
long long toDecimalCase(char *n1,long long radix){
	long long result = 0;
	long long length = 0;
	length = strlen(n1);
	long long tempNum;
	long long m = 1;
	for(int i = length - 1;i >= 0;i--){ //ways of converting,number contains {a-z}
		if(n1[i] >= 'a' && n1[i] <= 'z'){
			tempNum = n1[i] - 'a' + 10;
		}else if(n1[i] >= '0' && n1[i]<= '9'){
			tempNum = n1[i] - '0';
		}
		result += tempNum*m;
		m *= radix;
	}
	return result;
}
//find the minimum field of all numbers
long long findLowNum(char *n1){   
	long long length = 0;
	length = strlen(n1);
	long long tempNum;
	long long low = 0;
	for(int i = length - 1;i >= 0;i--){
		if(n1[i] >= 'a' && n1[i] <= 'z'){
			tempNum = n1[i] - 'a' + 10;
		}else if(n1[i] >= '0' && n1[i] <= '9'){
			tempNum = n1[i] - '0';
		}
		if(tempNum + 1 > low){
			low = tempNum + 1;
		}
	}
	return low;
}
 // compare number that has been convert to decimal with the array and radix.
long long compare(long long newNum,char *n1,long long mid){ 
	long long sum = 0;
	long long length = strlen(n1);
	long long temp = 0,m = 1;
	for(int i = length - 1;i >= 0;i--){
		if(n1[i] >= 'a' && n1[i] <= 'z'){
			temp = n1[i] - 'a' + 10;
		}else if(n1[i] >='0' && n1[i]<= '9'){
			temp = n1[i] - '0';
		}
		sum += temp*m;
		m *= mid;
		if(sum > newNum){
			return 1;
		}
	}
	if(sum > newNum){
		return 1;
	}else if(sum == newNum){
		return 0;
	}else{
		return -1;
	}
}
//binary search ,logn
long long binarySearch(char *n1,long long low,long long high,long long newNum){
	long long mid = low;
	long long tag;
	while(low <= high){
		tag = compare(newNum,n1,mid);
		switch(tag){
		case 1:
			high = mid -1;
			break;
		case 0:
			return mid;
			
		case -1:
			low = mid +1;
			
		}
		mid = (low + high)/2;
	}
	return -1;
}

int main(){
	char n1[11],n2[11],n3[11];
	int tag;
	long long radix;	
	long long result;
	scanf("%s %s %d %s",n1,n2,&tag,n3);
	radix = atoi(n3);
	long long newNum,low,high;
	switch(tag){
	case 1 :
		newNum = toDecimalCase(n1,radix);
		low = findLowNum(n2);
		high = (newNum+1 > low+1) ? newNum+1 : low+1;  
		result = binarySearch(n2,low,high,newNum);
		break;
	case 2 :
		newNum = toDecimalCase(n2,radix);
		low = findLowNum(n1);
		high = (newNum+1 > low+1) ? newNum+1 : low+1;  
		result = binarySearch(n1,low,high,newNum);
		break;
	}
	if(-1 != result){  
         printf("%ld\n", (long)result);  
    }  
    else {  
        printf("Impossible");  
    }  
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值