加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

计算完数的代码

(2010-10-26 19:41:01)
标签:

娱乐

    完数是指所有能被它整除的因子数的和等于它本身的数,我作为一个初学者编出了这个程序感到很自豪啊。
-------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
bool perfect(int x)
{
int step=1;
int a=0;
while (x>step)
      {
      if(x%step==0)
             {
             a=a+step;
             }
      step=step+1;
      }
if(x!=a)
     {
      return 0;
      }
else 
      {
       return 1;
       }
}

int main()
{
int top=1;
int step=1;
int bottom=999999;
int a;
int n;
int sum=0;
for(n=top;n<=bottom;n=n+step)
{
    a=perfect(n);
    if(a==1)
       {
        printf("%d是完数\n",n);
        sum=sum+1;
        }

}
printf("运算完毕\n");
printf("sum=%d",sum);
system ("pause");
return 0;

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有