📄 subject_54190.htm
字号:
<p>
序号:54190 发表者:yiguozhen 发表日期:2003-09-26 11:16:50
<br>主题:谁能帮我分析程序运行结果的错误?
<br>内容:以下是我编的一个求1000以内的完数的代码.(完数就是 :一个数刚好等于除自己本身之外的所有因子的和,比如6是完数6=1+2+3)<BR>#include<iostream><BR>class wanshu {<BR> int num;<BR> int a[50];<BR> int j;<BR> public:<BR> wanshu(int i):num(i),j(0){}<BR> ~wanshu(){}<BR> void func(void) {<BR> for(int i=1;i<=num/2;i++) {<BR> if(num%i==0) {<BR> a[j]=i;<BR> j++;<BR> }}<BR> int sum=0;<BR> for(int i=0;i<j;i++) {<BR> sum+=a[i];<BR> if(sum==num) {<BR> cout<<num<<"is a wanshu"<<endl;<BR> cout<<num<<"=";<BR> for(int i=0;i<j-1;i++)<BR> cout<<a[i]<<"+";<BR> cout<<a[j-1]<<endl;<BR> }}}};<BR> main() {<BR> for(int i=1;i<=1000;i++){<BR> wanshu w(i);<BR> w.func();<BR> // delete w;<BR>}}<BR>执行结果如下:<BR>[xx0048@yward c++]$ ./wanshu2.o<BR>6is a wanshu<BR>6=1+2+3<BR>24is a wanshu<BR>24=1+2+3+4+6+8+12<BR>28is a wanshu<BR>28=1+2+4+7+14<BR>496is a wanshu<BR>496=1+2+4+8+16+31+62+124+248<BR>[xx0048@yward c++]$<BR>从执行结果可以看出,程序已经求出了6 28 496是完数,但是为什么程序会错误地把24也当成是完数呢?24=1+2+3+4+6+8+12不会成立呀?为什么程序检查不出来呢?<BR>求教各位大哥!!<BR><BR><BR><BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:iwill 回复日期:2003-09-26 11:30:29
<br>内容:求和没有全部计算完毕就判断是否等于本身<BR><BR>24=1+2+3+4+6+8+12<BR> |<BR> 加到这儿就等于本身了,你的程序不再继续求和直接输出了.<BR><BR><BR>你的程序风格太#$%#^#@$%@$#%#了 呵呵:<BR><BR>#include <iostream.h><BR><BR>class wanshu <BR>{<BR> int num;<BR> int a[50];<BR> int j;<BR>public:<BR> wanshu(int i):num(i),j(0){}<BR> ~wanshu(){}<BR> void func(void) <BR> {<BR> for(int i=1;i<=num/2;i++)<BR> {<BR> if(num%i==0) <BR> {<BR> a[j]=i;<BR> j++;<BR> }<BR> }<BR> int sum=0;<BR> for(i=0;i<j;i++)<BR> {<BR> sum+=a[i];<BR> <BR> }<BR> if(sum==num)<BR> {<BR> cout<<num<<"is a wanshu"<<endl;<BR> cout<<num<<"=";<BR> for(i=0;i<j-1;i++)<BR> cout<<a[i]<<"+";<BR> cout<<a[j-1]<<endl;<BR> }<BR> }<BR>};<BR>main()<BR>{<BR> for(int i=1;i<=1000;i++)<BR> {<BR> wanshu w(i);<BR> w.func();<BR> }<BR>}<BR> <BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:yiguozhen 回复日期:2003-09-26 11:30:30
<br>内容:我觉得很奇怪,一般来说,一个算法如果能算出大部分的问题,应该是要能解决一般的问题才对,怎么怪了?中间出了错误?<BR>难道是我的算法有问题吗?如果有,为什么它能算出3个完数出来?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:yiguozhen 回复日期:2003-09-26 11:38:58
<br>内容:to :iwill<BR>我也知道<BR>24=1+2+3+4+6+8+12<BR> |<BR> 加到这儿就等于本身了,你的程序不再继续求和直接输出了.<BR><BR>问题是为什么会这样?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:红蚂蚁 回复日期:2003-09-26 11:40:58
<br>内容:判断相等应该在求和完成之后
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:红蚂蚁 回复日期:2003-09-26 11:43:08
<br>内容:#include "iostream.h"<BR><BR>class wanshu <BR>{<BR> int num;<BR> int a[50];<BR> int j;<BR>public:<BR> wanshu(int i):num(i),j(0){}<BR> ~wanshu(){}<BR> void func(void) <BR> {<BR> for(int i=1;i<=num/2;i++) <BR> {<BR> if(num%i==0) <BR> {<BR> a[j]=i;<BR> j++;<BR> }<BR> }<BR> int sum=0;<BR> for(i=0;i<j;i++) <BR> {<BR> sum+=a[i];<BR> }<BR> if(sum==num) <BR> {<BR> cout<<num<<"is a wanshu"<<endl;<BR> cout<<num<<"=";<BR> for(int i=0;i<j-1;i++)<BR> cout<<a[i]<<"+";<BR> cout<<a[j-1]<<endl;<BR> }<BR> }<BR>};<BR><BR>main() <BR>{<BR> for(int i=1;i<=1000;i++)<BR> {<BR> wanshu w(i);<BR> w.func();<BR> // delete w;<BR> }<BR> return;<BR>}
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:yiguozhen 回复日期:2003-09-26 11:47:22
<br>内容:楼上说的是.程序改为:<BR>#include<iostream><BR>class wanshu {<BR> int num;<BR> int a[50];<BR> int j;<BR> public:<BR> wanshu(int i):num(i),j(0){}<BR> ~wanshu(){}<BR> void func(void) {<BR> for(int i=1;i<=num/2;i++) {<BR> if(num%i==0) {<BR> a[j]=i;<BR> j++;<BR> }}<BR> int sum=0;<BR> for(int i=0;i<j;i++) {<BR> sum+=a[i];}//*这里做了改动 <BR> if(sum==num) {<BR> cout<<num<<"is a wanshu"<<endl;<BR> cout<<num<<"=";<BR> for(int i=0;i<j-1;i++)<BR> cout<<a[i]<<"+";<BR> cout<<a[j-1]<<endl;<BR> }}};//*这里做了改动<BR>main() {<BR>for(int i=1;i<=1000;i++){<BR> wanshu w(i);<BR> w.func();<BR>// delete w;<BR>}}<BR>执行结果:<BR>[xx0048@yward c++]$ g++ wanshu2.cpp -o wanshu2.o<BR>[xx0048@yward c++]$ ./wanshu2.o<BR>6is a wanshu<BR>6=1+2+3<BR>28is a wanshu<BR>28=1+2+4+7+14<BR>496is a wanshu<BR>496=1+2+4+8+16+31+62+124+248<BR>[xx0048@yward c++]$<BR>好极了!!!!!<BR>2003-9-26 11:49:36
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -