求完数

Woodz 2011-04-17 09:55:26
#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0;
int y;
int i = 0; //完数的个数

printf("请依次输入x,n的值(用空格隔开),我会为你输出x~n的完数。\n");
scanf("%d %d", &x, &n);
x_j = x;

if(x > n)
{
printf("输入错误,x应小于n。请重新输入。\n");
scanf("%d %d", &x, &n);
x_j = x;
}

for(; x <= n; x++)
{
for(y = 1; y <= sqrt(n); y++)
{
if(x % y == 0)
{
sum = sum + y + x / y;
if(y == sqrt(n))
{
sum -= y;
break;
}
}

else
continue;
}

if(sum == x)
{
printf("%d\n 是完数。\n", x);
i++;
}

else
continue;
}

printf("\n经统计,%d~%d一共有 %d 个完数。\n", x_j, n, i);

getchar();
return 0;
}


不知道哪里搞错了,总是没结果……
...全文
168 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Woodz 2011-04-17
  • 打赏
  • 举报
回复
汗…… 搜索了一下,1的确不是
[Quote=引用 12 楼 luohaohahaha 的回复:]

引用 11 楼 free4537 的回复:

C/C++ code

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum;
int sum……
[/Quote]
Woodz 2011-04-17
  • 打赏
  • 举报
回复
最后一句x~n,因为x变了[Quote=引用 3 楼 ulfsaar 的回复:]

//x_j = x;
这句不能注释,没看到你后面有用到
[/Quote]
luohaohahaha 2011-04-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 free4537 的回复:]

C/C++ code

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum;
int sum……
[/Quote]
唉 lz 1不是完数 ,不认真看我的注释。
Woodz 2011-04-17
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum;
int sum_j; //寄存sum
int y;
int i = 0; //完数的个数

printf("请依次输入x,n的值(用空格隔开),我会为你输出x~n的完数。\n");
scanf("%d %d", &x, &n);
x_j = x;

while(x > n || n < 0 || x < 0)
{
printf("输入错误,x应小于n。请重新输入。\n");
scanf("%d %d", &x, &n);
x_j = x;
}

for(; x <= n; x++)
{
sum = 0;

for(y = 2; y <= sqrt(x); y++) //从2开始
{
if(x % y == 0)
{
sum += y + x/y;

if(y == sqrt(x))
sum -= y;
}

else
continue;
}

sum += 1; //1是每个数的因子

if(sum == x)
{
printf("\n%d 是完数。", x);
i++; //完数个数
}

else
continue;
}


printf("\n\n经统计,%d~%d一共有 %d 个完数。\n", x_j, n, i);

getchar();
return 0;
}


已完善,呵呵~
还有,1应该也算完数吧
Woodz 2011-04-17
  • 打赏
  • 举报
回复
兄弟,你搞错了,我就是楼主……不过感觉这样做循环次数会少些,呵呵[Quote=引用 7 楼 happymawolf 的回复:]

引用 4 楼 luohaohahaha 的回复:
C/C++ code

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0……

……
[/Quote]
Ulfsaar 2011-04-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 luohaohahaha 的回复:]
C/C++ code

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0……
[/Quote]

可以这么改,y需从2开始,否则就会加上自己(y/1 == y)

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0;
int y;
int i = 0; //完数的个数

printf("请依次输入x,n的值(用空格隔开),我会为你输出x~n的完数。\n");
scanf("%d %d", &x, &n);
x_j = x;

if(x > n)
{
printf("输入错误,x应小于n。请重新输入。\n");
scanf("%d %d", &x, &n);
x_j = x;
}

for(; x <= n; x++)
{
sum =1;
for(y = 2; y <= sqrt(x); y++)
{
if(x % y == 0)
{
sum = sum + y + x / y;
if(y == sqrt(x))
{
sum -= y;
}
}
}

if(sum == x)
{
printf("%d\n 是完数。\n", x);
i++;
}

else
continue;
}

printf("\n经统计,%d~%d一共有 %d 个完数。\n", x_j, n, i);

getchar();
getchar();
return 0;
}
书虫 2011-04-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hhh_hao 的回复:]
C/C++ code

  求1000以内的完数的C语言代码如下:
  #include <stdio.h>
  int main()
  {
  int n=1000;
  int r,j,i;
  for(i=1;i<n; i++){
  r = 0;
  for(j=1;j<i;j++){
  if(i%j == 0){
  r = r + j;
  }
  }
……
[/Quote]
你百度的狠哪,哈哈
http://baike.baidu.com/view/640632.htm#sub640632
书虫 2011-04-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 luohaohahaha 的回复:]
C/C++ code

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0……
[/Quote]
⊙﹏⊙b汗,兄弟比我快些!英雄所见略同啊!哈哈
书虫 2011-04-17
  • 打赏
  • 举报
回复
贴主的算法有点问题,帮你改了下

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0;
int y;
int i = 0; //完数的个数

printf("请依次输入x,n的值(用空格隔开),我会为你输出x~n的完数。\n");
scanf("%d %d", &x, &n);
x_j = x;

if(x > n)
{
printf("输入错误,x应小于n。请重新输入。\n");
scanf("%d %d", &x, &n);
x_j = x;
}

for(; x <= n; x++)
{
sum = 0;
for(y = 1; y <= x/2; y++)
{
if(x % y == 0)
{
sum = sum + y /*+ x / y*/;
// if(y == sqrt((float)n))
// {
// sum -= y;
// break;
// }
}

// else
// continue;
}

if(sum == x)
{
printf("%d\n 是完数。\n", x);
i++;
}

else
continue;
}

printf("\n经统计,%d~%d一共有 %d 个完数。\n", x_j, n, i);

getchar();
return 0;
}
Woodz 2011-04-17
  • 打赏
  • 举报
回复


for(y = 1; y < x; y++)//此处上限应该是x
{
if(x % y == 0)
{
sum = sum + y;//累加每个因子
}
}

这儿用我的方法不行吗?如9则计算 sum = (1 + 9) + (3 + 3),3 == sqrt(9) 则 sum -= 3
10 1 10 2 5 2 != sqrt(10) sum 不变 [Quote=引用 2 楼 ulfsaar 的回复:]

C/C++ code

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0;
int y;
int i = 0;……
[/Quote]
luohaohahaha 2011-04-17
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0;
int y;
int i = 0; //完数的个数

printf("请依次输入x,n的值(用空格隔开),我会为你输出x~n的完数。\n");
scanf("%d%d", &x, &n);
x_j = x;

if(x > n)
{
printf("输入错误,x应小于n。请重新输入。\n");
scanf("%d %d", &x, &n);
x_j = x;
}

for(; x <= n; x++) //完数从2开始。
{
sum = 0;
for(y = 1; y <= x/2; y++) // ——————y<=x/2
{
if(x % y == 0)
{
sum = sum + y ;
}

else
continue;
}

if(sum == x)
{
printf("%d\n 是完数。\n", x);
i++;
}

else
continue;
}

printf("\n经统计,%d~%d一共有 %d 个完数。\n", x_j, n, i);

getchar();
return 0;
}

lz试试这个、
Ulfsaar 2011-04-17
  • 打赏
  • 举报
回复
//x_j = x;
这句不能注释,没看到你后面有用到
Ulfsaar 2011-04-17
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<math.h>

int main(void) /*求完数。完数:如28 = 1 + 2 + 4 + 7 + 14,完数等于
其各因子之和*/
{
int x;
int x_j; //寄存x的值
int n;
int sum = 0;
int y;
int i = 0; //完数的个数

printf("请依次输入x,n的值(用空格隔开),我会为你输出x~n的完数。\n");
scanf("%d %d", &x, &n);
x_j = x;

while(x > n || x <0 || n<0)//循环输入
{
printf("输入错误,x应小于n。请重新输入。\n");
scanf("%d %d", &x, &n);
//x_j = x;
}

for(; x <= n; x++)
{
sum = 0;//每计算一个数要清零
for(y = 1; y < x; y++)//此处上限应该是x
{
if(x % y == 0)
{
sum = sum + y;//累加每个因子
}
}

if(sum == x)
{
printf("%d\n 是完数。\n", x);
i++;
}

else
continue;
}

printf("\n经统计,%d~%d一共有 %d 个完数。\n", x_j, n, i);

getchar();
getchar();
return 0;
}
hhh_hao 2011-04-17
  • 打赏
  • 举报
回复

  求1000以内的完数的C语言代码如下:
  #include <stdio.h>
  int main()
  {
  int n=1000;
  int r,j,i;
  for(i=1;i<n; i++){
  r = 0;
  for(j=1;j<i;j++){
  if(i%j == 0){
  r = r + j;
  }
  }
  if(r == i){
  printf("the result is:%d\n",r);
  }
  }
  return 0;
  }
  输出结果为:
  6,28,496,
  即1000以内的完数只有6、28、496三个数字。


33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧