求一个不大于十万的数的位数 请问高手哪错了?

作者在 2010-09-15 15:00:24 发布以下内容
#include<stdio.h>
#include<math.h>
void main()
{
    double i,a;int m=0;
     printf("Input a number LE 100000\n");
  scanf("%d",&a);
  for(i=1;i<6;i++){
   if(pow(10,i)>a)
   {m++; printf("a is a %d-digit number\n",m);break;}
  }
}
默认分类 | 阅读 998 次
文章评论,共7条
jiou
2010-09-18 13:53
1
#include&lt;stdio.h&gt;<br />
#include&lt;math.h&gt;<br />
int main()<br />
{<br />
&nbsp;&nbsp;double a;<br />
&nbsp;&nbsp;int m=0;<br />
&nbsp;&nbsp;printf(&quot;Input a number LE 100000\n&quot;);<br />
&nbsp;&nbsp;scanf(&quot;%lf&quot;,&amp;a);<br />
&nbsp;&nbsp;while(1)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
 &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; m++;<br />
&nbsp; &nbsp;&nbsp; &nbsp; if(pow(10.0,m)&gt;a)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
 &nbsp; &nbsp; &nbsp; &nbsp; }<br />
 &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;a is a %d -digit number\n&quot;,m);<br />
&nbsp; &nbsp; &nbsp; &nbsp; return 0;<br />
}<br />
<br />
<br />
<br />
自己看去吧!你在这个scanf(&quot;%d&quot;,&amp;a);<br />
就犯了一个很严重的错误应该为scanf(&quot;%lf&quot;,&amp;a);
kuailehg
2010-09-19 23:04
2
lz 你的思路有点不对哦,for循环,直接就定位了你收入的数是一个五位数,不行的哦!看看我写的吧!你这个题可以用好多种方法做,二楼的老表说的几点错误也很好哦,以后要避免哦~!<br />
程序:<br />
#include&lt;stdio.h&gt;<br />
void main()<br />
{<br />
&nbsp; &nbsp; int i,m=1;double a;<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;Input a number 小于 100000\n&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; scanf(&quot;%lf&quot;,&amp;a);<br />
&nbsp; &nbsp; &nbsp; &nbsp; while(a&gt;=10)<br />
&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;a/=10;<br />
&nbsp; &nbsp;&nbsp; &nbsp;m++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;m=%d\n&quot;,m);<br />
}
tingfeng_bcn
2010-10-05 19:36
3
您应该学习一下二楼同学的编程风格,还有三楼同学的算法实现。
帆乐2010(作者)
2010-10-09 11:30
4
Sorry, I'm late. Thanks a lot for your directions.
帆乐2010(作者)
2010-10-09 11:42
5
<div class="quote"><span class="q"><b>tingfeng_bcn</b>: 您应该学习一下二楼同学的编程风格,还有三楼同学的算法实现。</span></div>And also accumulated experience as you.Thx .
帆乐2010(作者)
2010-10-09 11:43
6
#include&lt;stdio.h&gt;<br />
#include&lt;math.h&gt;<br />
void main()<br />
{<br />
&nbsp; &nbsp; double i=0.0,a;int m=0;<br />
&nbsp; &nbsp;&nbsp;&nbsp;printf(&quot;Input a positive integer LE 1.7e308\n&quot;);<br />
&nbsp;&nbsp;scanf(&quot;%lf&quot;,&amp;a);<br />
&nbsp;&nbsp;while(i&lt;a)<br />
 {<br />
&nbsp; &nbsp;m++;<br />
&nbsp; &nbsp;i=pow(10.0,m);<br />
&nbsp;&nbsp;}<br />
&nbsp; &nbsp;printf(&quot;a is a %d-digit number\n&quot;,m);<br />
&nbsp;&nbsp;}
帆乐2010(作者)
2010-10-09 11:52
7
<div class="quote"><span class="q"><b>kuailehg</b>: lz 你的思路有点不对哦,for循环,直接就定位了你收入的数是一个五位数,不行的哦!看看我写的吧!你这个题可以用好多种方法做,二楼的老表说的几点错误也很好哦</span></div><img src="image/face/22.gif" class="face">
游客请输入验证码
文章分类