topic
Description
Find the complete number within 1-n. The so-called complete number is a number whose all factors add up to itself. For example, 6 has 3 factors 1,2,3,1+2+3=6, then 6 It's over. That is, the final number is equal to the sum of all its factors.
Input
There are multiple sets of test data, and the input n, n data range is not large.
Output
For each group of input, please output all the numbers within 1-n. If there are multiple numbers in the output of a case, separate them with spaces, and do not have extra spaces at the end of the output.
Sample Input Copy
6
Sample Output Copy
6
Ideas
- There is nothing to say, just find the factor of 1-n/2 and then sum (the maximum factor must be n/2 (the smallest factor))
- If you want to save time, you can make a list of the completed numbers
Code
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
vector<int> full;//完数打表
const int maxn=10005;
int main(){
int n=0;
while(scanf("%d",&n)!=EOF){
if(!full.empty()&&n<=full[full.size()-1]){
for(int i=0;i<full.size()&&full[i]<=n;i++)
printf("%d ",full[i]);
printf("\n");
continue;
}
for(int i=0;i<full.size()&&full[i]<=n;i++)
printf("%d ",full[i]);
int start=3;
if(!full.empty())
start=full[full.size()-1]+1;
for(int j=start;j<=n;j++){
int sum=0;
for(int i=1;i<=j/2;i++){
if(j%i==0){
sum+=i;
}
}
if(sum==j){
printf("%d ",j);
full.push_back(j);
}
}
printf("\n");
}
return 0;
}