我正在做一个练习,我必须编写一个未签名的 C++ 中的函数 --- 该函数返回了位数 in num 并且必须处理任何数字。
The issue --- when I do unsigned num is greater than 10 digits, it still
shows 10 as the answer. what am I doing wrong?
unsigned numDigits(unsigned num)
{
if (num == 0)
return 0;
return 1 + numDigits(num / 10);
return (num);
}
int main()
{
unsigned num = 12345678901;
cout << "Number of Digits: " << numDigits(num);
}
最佳答案
无符号大小为:
0 to 65,535 or
0 to 4,294,967,295 (10 digits)
所以把你的函数改成:
unsigned numDigits(long long unsigned num)
您的问题的作弊解决方案也是:
std::string dig = std::to_string(num);
std::cout << dig.size();
关于c++ - 使用任何数字返回数字的无符号函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509470/