间限制 : 1.000 sec 内存限制 : 32 MB
题目描述
求1-n内的完数,所谓的完数是这样的数,它的所有因子相加等于它自身,比如6有3个因子1,2,3,1+2+3=6,那么6是完数。即完数是等于其所有因子相加和的数。
输入
测试数据有多组,输入n,n数据范围不大。
输出
对于每组输入,请输出1-n内所有的完数。如有案例输出有多个数字,用空格隔开,输出最后不要有多余的空格。
样例输入 Copy
6
样例输出 Copy
6
#include <iostream>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int isperfect(int x){
int sum = 0;
for(int i = 1; i <= x / 2; i++){
if(x%i == 0) sum +=i;
}
if(sum == x) return 1;
else return 0;
}
int main(int argc, char** argv) {
int n;
while(cin >> n) {
int flag = 1;
for(int i = 6; i <= n; i++){
if(isperfect(i)){
if(flag){
cout << i;
flag = 0;
}else{
cout << " " << i;
}
}
}
cout << endl;
}
return 0;
}
606

被折叠的 条评论
为什么被折叠?



