问题描述
如何在不使用字符串或数组的情况下从base n转换为base 10(其中n是2到9之间的输入)?转换的数字是一个长整数类型。
我试过的代码返回输入到函数中的相同数字(num)。
我尝试过:
int calcBase10(long long num,int base) )
{
int base10;
int商;
int余数;
if(base == 10)
{
base10 = num;
return(base10);
}
商= num / base;
余数= num%base;
if(商= = 0)
{
base10 =余数;
}
else
{
base10 =(base * calcBase10(商,基数)+余数);
}
return( base10);
}
How do I convert from base n to base 10 without using strings or arrays(where n is an input between 2 and 9)? The number being converted is a long long integer type.
The code that I have tried returns the same number (num) that was input into the function.
What I have tried:
int calcBase10(long long num, int base)
{
int base10;
int quotient;
int remainder;
if(base == 10)
{
base10 = num;
return(base10);
}
quotient = num / base;
remainder = num % base;
if(quotient == 0)
{
base10 = remainder;
}
else
{
base10 = (base * calcBase10(quotient, base) + remainder);
}
return(base10);
}
推荐答案
这篇关于如何在不使用字符串或数组的情况下从base n转换为base 10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!