【第九周编程题】

妙妙啦 发表于2016年11月16日
<p>第一题要怎么创建函数呢&nbsp;</p><p>从键盘输入一个数,检查这个数中是否有重复出现的数字。如果这个数中有重复出现的数字,则显示“Repeated digit!”;否则显示“No repeated digit!”。</p><p>已知函数原型:</p><p><br ></p><p>int CountRepeatNum(int count[], int n);</p><p>若有重复数字,则该函数返回重复出现的数字;否则返回-1.</p><p><br ></p><p>其中int count[]和int n分别传递的是什么呢。</p><p>我用了别的方法做出来了,下面是我的代码,但是我还是想知道根据题目要求的话要怎么做?</p><p><code class="brush:cpp;toolbar:false" >#include&nbsp;&lt;stdio.h&gt; int&nbsp;main() { long&nbsp;int&nbsp;n&nbsp;=&nbsp;0; int&nbsp;count[10]={0},i&nbsp;=&nbsp;0,flag&nbsp;=&nbsp;0; printf(&quot;Input&nbsp;n:\n&quot;);&nbsp; scanf(&quot;%ld&quot;,&amp;n); while(n)&nbsp;&nbsp;/*将n的每个数都求出来并分别存入对应的数组中*/ { for(i&nbsp;=&nbsp;0;i&lt;10;i++) { if(i==(n%10)) { count[i]++; } } n&nbsp;/=10; } for(i&nbsp;=&nbsp;0;i&lt;10;i++) { if(count[i]&gt;=2)/*判断是否有重复的数字*/ { flag&nbsp;=&nbsp;1; break; }&nbsp; } if(flag==1) printf(&quot;Repeated&nbsp;digit!\n&quot;); else printf(&nbsp;&quot;No&nbsp;repeated&nbsp;digit!\n&quot;)&nbsp;; return&nbsp;0; }</code></p>
1 回复