JDOJ 1140: End number

JDOJ 1140: End number

Topic Portal

Description

If a number is exactly equal to the sum of its factors, this number is called "count them." For example, a factor of 6, 2,3, and 6 + 2 + 1 = 3, 6 is therefore "count them." Programmed to find all of the number N in the finished, press the following factors which output format:

Input

N

Output

? its factors are ? ? ?

Sample Input

1000

Sample Output

6 its factors are 1 2 3 28 its factors are 1 2 4 7 14 496 its factors are 1 2 4 8 16 31 62 124 248

answer:

A very simple simulation, operations related to the base is determined prime number and prime factor decomposition.

Can think, required number of complete to meet two conditions, first, with a bunch of quality factor, the second, the quality factor is equal to the sum of the original number.

So we can draw ideas: to maintain the quality factor sequence, respectively (opening array), and the quality factor of each decomposed addition, if equal to (matches), can be output.

Note that the card format is JDOJ glorious tradition, so we have to add the last sentence special format.

Code:

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int tmp;
    int ram[10001];
    int cnt;
    for(int i=1;i<=n;i++)
    {
        cnt=0;
        tmp=0;
        for(int j=1;j<=i/2;j++)
            if(i%j==0)
            {
                ram[++cnt]=j;
                tmp+=j;
            }
        if(tmp==i)
        {
            printf("%d its factors are ",i);
            for(int j=1;j<=cnt;j++)
                printf("%d ",ram[j]);
            cout<<endl;
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11294003.html