第一种:数字分割法
代码如下:
#include <stdio.h>
void main()
{
int x,b=0;
scanf("%d",&x);
while(x>0)
{
x=x/10;
b++;
}
printf("%d ",b);
}
第二种:log10法
C语言中只有log和log10两种函数。
如果想表达log a,b 那么可以使用log(b)/log(a)来解决。
代码如下:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<(int)(log10(n))+1;
return 0;
}
本文介绍了两种在C语言中计算整数位数的方法:数字分割法和log10法。数字分割法通过循环除以10直到数字变为0来计数位数;log10法则利用数学库函数log10计算位数。

被折叠的 条评论
为什么被折叠?



