遇见你的 “完数”——每天学习分享Day3

今天的你遇见了什么呢?一起来看看 “完数” 是什么吧~

题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
简要分析:通过循环找到一个数的所有因数,并将因数放入数组中,确认是否为完数在于判断该数组中的因数之和是否等于该数。

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int i, n, t = 0;
	int a[1000];
	
	for(n = 2; n < 1000; n++){
		t=0; 
		for(i = 1; i < n; i++){
			if((n % i) == 0)
				a[i-1] = i;
			else 
				a[i-1] = 0;
			t += a[i-1];			
		}
		if(n == t)
			printf("%d ",n);		 //在内循环外执行,你能想明白的!
	} 
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44566432/article/details/87944210