c c++ 언어 공부

백준 9506번 : 약수들의 합 C언어

Code C 2023. 3. 5. 21:59

https://www.acmicpc.net/problem/9506 : 백준 문제 링크

 

Code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h> 
 
int main()
{
    int n;
    while (1)
    {
        scanf("%d"&n);
        int arr[100000= { 0, };
        if (n == -1)
        {
            break;
        }
        int sum = 0;
        int count = 0;
        for (int i = 1; i < n; i++)
        {
            if (n % i == 0)
            {
                arr[count++= i;
                sum += i;
            }
        }
        int last = count;
        if (sum == n)
        {
            printf("%d = ", n);
            for (int i = 0; i < last; i++)
            {
                if (i==(last - 1))
                {
                    printf("%d\n", arr[i]);
                }
                else
                {
                    printf("%d + ", arr[i]);
                }
            }
        }
        else
        {
            printf("%d is NOT perfect.\n", n);
        }
    }
}
cs

while(1)을 이용해서 -1이 들어오기 전까지 반복함.

n의 입력전까지 for문으로 약수와 개수를 확인 후

완전수인지 아닌지를 판단하여 출력함.