|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求助各位为啥下面这个程序统计不出一个数字的位数n
#include<stdio.h>
int main()
{
int i;
scanf("%d",&i);
int shitan=1;
int n=0;
while(shitan!=0)
{
shitan=i/ 10^n;
n=n+1;
}
printf("%d\n",n);
return 0;
}
你的代码不能实现统计的功能是因为很多原因,10的n次方使用pow()函数等等。
还有上一个回答问题的鱼油代码没有考虑只输入0的情况。
你可以参考一下我写的代码,可能不够完善,不是最优的方案,但是基本你的要求可以实现。
- #include<stdio.h>
- #include<math.h>
- int main()
- {
- int i,shitan = 1,n = 0;
- scanf("%d",&i);
- if (i/10 == 0)
- {
- printf("有1位数。");
- }
- else
- {
- do
- {
- i = i/10;
- n += 1;
- }while (i != 0);
- printf("有%d位数。",n);
- }
- return 0;
- }
复制代码
|
|