当前位置: 代码迷 >> C语言 >> 再问一道,都是我的作业题
  详细解决方案

再问一道,都是我的作业题

热度:359   发布时间:2004-10-04 20:10:00.0
再问一道,都是我的作业题
Greek mathematicians took special interest in integers that are equal to the sum of their proper divisors (a
proper divisor of an integer nis any divisor strictly less than nitself.) They called such numbers perfect
numbers. For example, 6is a perfect number because it is the sum of 1, 2, and 3, which are the
integers less than 6that divide evenly into 6. Similarly, 28is a perfect number because its proper divisors
are 1, 2, 4, 7, and 14.
Write a predicate function IsPerfectwhich takes an integer nas its only argument and returns TRUEif
and only if nis perfect, and FALSEotherwise. (Sadly enough, this problem has no fast food references.)
/*
*Function:IsPerfect
*if(IsPerfect(number)){
*-------------------
*IsPerfectreturnsTRUEifandonlyifthethespecifiedintegerisa
*perfectinteger.Anumberisperfectifandonlyifitequalsthesum
*ofitsproperdivisors.Thespecifiedintegerisassumedtobe
*positive,andnoerrorcheckingisperformed.
*/
boolIsPerfect(intn)
搜索更多相关的解决方案: 作业  divisor  perfect  proper  numbers  

----------------解决方案--------------------------------------------------------

我其实就是想问

perfect number就是,一个数的约数之和等于他本身。 比如6=1+2+3 28=1+2+4+7+14 我现在需要用这个函数 bool IsPerfect(int n) 接下来该写什么?用for 里面套if吗?怎么用呢?


----------------解决方案--------------------------------------------------------

还好我懂点中文

基本框架:

int i,s=0;

for(i=1;1<n/2;i++)if(n%i==0)s+=i;

return !(s-n);


----------------解决方案--------------------------------------------------------
谢谢哦,我写出来了再来谢你
----------------解决方案--------------------------------------------------------
!(s-n);代表什么
----------------解决方案--------------------------------------------------------

返回值

if 这个数的约数只和等于这个数 !(s-n)==1;

else !(s-n)==0;


----------------解决方案--------------------------------------------------------
为什么返回值用这个呢
----------------解决方案--------------------------------------------------------
why use english?
----------------解决方案--------------------------------------------------------
our teacher use it
----------------解决方案--------------------------------------------------------
  相关解决方案