下面的函数应该用来计算数字中的位数。不幸的是,它不适用于11位或更多的数字,我不知道为什么。我觉得这和数据类型有关,但我认为long long int在这种情况下是可以的。谢谢你的帮助!
long long int getLength(long long int input)
{
long long int length = 0;
while(input != 0)
{
input /= 10;
++length;
}
return (length);
}
最佳答案
这可能不是一个答案,但请允许我在此报告输出。你能再核对一下吗?它对我有利。
#include "stdio.h"
long long int getLength(long long int input)
{
long long int length = 0;
while(input != 0)
{
input /= 10;
++length;
}
return (length);
}
int main()
{
printf("%lld\n", getLength(12345678901)); // 11
printf("%lld\n", getLength(123456789012)); // 12
printf("%lld\n", getLength(1234567890123)); // 13
printf("%lld\n", getLength(0)); // 0
printf("%lld\n", getLength(-123)); // 3
}
平台Windows 10,和
gcc --version
返回gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 7.3.0
问题1:您真的需要输入
long long int
来报告位数吗?问题2:您在
%lld
中是否正确使用了格式说明符printf
?关于c - C编程的数字计数功能将无法使用11位以上的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53111930/