完美數

 avatar
user_3763047219
c_cpp
2 years ago
575 B
2
Indexable
Never
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
int main()
{
	int N = 0, ans[10000] = { 0 }, count = 0, ans2[10000] = { 0 };
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) {
		int  perfect = 0;
		for (int j = 1; j <= i; j++) {
			if (i % j == 0 && j != i) {
				perfect = perfect + j;
			}
		}
		ans[i] = perfect;
	}

	for (int i = 1; i <= N; i++) {
		if (ans[i] == i) {
			count = count + 1;
			ans2[count] = i;
		}
	}

	if (count != 0) {
		for (int i = 1; i < count; i++) {
				printf("%d ", ans2[i]);
		}
		printf("%d", ans2[count]);
	}
}