问题
Possible Duplicate:
Efficient algorithm for conversion between numeral system
Given an integer, write a program that converts the given number to a number (in base 10). Hint - The given number could be in any base, but the base is unknown.
回答1:
That can't be done; without knowing the source base the number is ambiguous. 10
in base n
translates to n
in base 10
; there are infinite possibilities
回答2:
I'm assuming by 'unknown' you mean the algorithm needs to be able to handle any base? Otherwise it's just plain impossible.
So you're basically asking for function convert(number, base) = base10Number?
count = 0
total = 0
for each digit in number, from least significant to most significant
total = total + digit * base^count
count = count + 1
e.g. convert(355,8)
- first loop: total = 0 + 5 * 8^0 = 5
- second loop: total = 5 + 5 * 8^1 = 45
- third loop: total = 45 + 3 * 8^2 = 237
Result = 237
回答3:
You can just use Wallar's algorithm to convert the base. The algorithm changes n base c into n base b. n is a list of the digits making up the number. Each digit may contain more than one digit. Below is an implementation of Wallar's algorithm in Python.
from math import *
def baseExpansion(n,c,b):
j = 0
base10 = sum([pow(c,len(n)-k-1)*n[k] for k in range(0,len(n))])
while floor(base10/pow(b,j)) != 0: j = j+1
return [floor(base10/pow(b,j-p)) % b for p in range(1,j+1)]
回答4:
It is easy to do, once you've got the base.
You can get a lower bound for the base, by finding the highest digit. Like in the number 175234 the base must be at least 8. However you can never find an upper bound: The number could be any base from 8 to infinity.
Instead you can print out the number it would be, given the first base was e.g. 8, 9 or 10. Then the user can decide what he/she thinks.
回答5:
This is wrong question because consider that number 7 it may be in octal system , hexadecimal system .It is not possible to decide .We must know input numbers base . We can write method like this
public int convertToBase(int inNumber , int inBase , int outBase){
// blah blah
return convertedNumber; }
回答6:
public class TestNumberBase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(converNumberTObase(100000, 2, 16));
}
public static int converNumberTObase(int inNum, int inBase, int outBase) {
return convertDecimalToOtherBase(convertDecimalEquivalent(inNum, inBase), outBase) ;
}
public static int convertDecimalEquivalent(int number, int inBase) {
int outNumber = 0;
int _base =inBase;
while (number > 0) {
int digit = number % 10;
number = number / 10;
outNumber = outNumber + (inBase / _base) * digit;
inBase = inBase*_base;
}
return outNumber;
}
public static int convertDecimalToOtherBase(int number, int outBase) {
int outNumber = 0;
int _base = 10, base =10;
while (number > 0) {
int digit = number % outBase;
number = number / outBase;
outNumber = outNumber + (base / _base) * digit;
base = base*_base;
}
return outNumber;
}
}
回答7:
The problem statement states that the base of the given number is unknown. Thus to proceed one must need to assume a base for the number. It is practically safe to assume that the digit with the maximum value in the number denotes the maximum that can be accounted in the unknown base. This number, for example if stated as, 254, it can be assumed that the number system consists of digits 0, 1, 2, 3, 4, 5 - or base 6.
if(!(((ascii >= '0') && (ascii <= '9')) || ((ascii >= 'A') && (ascii <= 'Z')))) {
printf("Illegal number, can have only digits (0-9) and letters (A-Z)");
Hope this helps.
来源:https://stackoverflow.com/questions/2927009/algorithm-to-convert-a-number-in-an-unknown-base-to-the-equivalent-base-10-numbe