求一个整数的位数时,每次丢弃个位数字,计数器加1.当该整数为0时,所得计数器的数值即为该整数的位数;其中n/10是求个位数字。(测试数字时尤其注意0这个数字)
#define _CRT_SECURE_NO_WARNINGS #includeint main() { long long n; int count = 0; scanf("%lld", &n); if (n == 0) { printf("1");//测试0 return 0; } while (n != 0) { count++; n /= 10;//丢弃个位数字 } printf("%dn", count); return 0; }
由于long long int类型的字节长度限制,并且整数具有符号,所以该程序最大只能显示输出19位位数 。



