Study/Baekjoon

[백준/C++] 9506 약수들의 합

에린_1 2024. 2. 19. 17:18
728x90

9506 약수들의 합

#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	
	while (true)
	{
		int num;
		int check_num = 0;
		vector<int> num_arr;
		cin >> num;
		if (-1 == num)
			break;
		for (int i = 1; i <= num; ++i)
		{
			if (0 == num % i)
			{
				num_arr.push_back(i);
				check_num += i;
			}
		}
		if (check_num - num_arr.back() == num)
		{
			cout << num << " = ";
			for (int idx = 0; idx < num_arr.size() - 1; ++idx)
			{
				if (idx == num_arr.size() - 2)
					cout << num_arr[idx] << "\\n";
				else
					cout << num_arr[idx] << " + ";
			}
		}
		else
			cout << num << " is NOT perfect." << "\\n";

	}
	return 0;
}
  • 약수문제를 풀었다면 똑같이 접근해서 풀 수 있다.

 

728x90

'Study > Baekjoon' 카테고리의 다른 글

[백준/C++] 2798 블랙잭  (0) 2024.02.20
[백준/C++] 11653 소인수분해  (0) 2024.02.20
[백준/C++] 2501 약수 구하기  (0) 2024.02.19
[백준/C++] 5086 배수와 약수  (0) 2024.02.19
[백준/C++] 2563 색종이  (0) 2024.02.19