Study/Baekjoon

[백준/C++] 2501 약수 구하기

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

2501 약수 구하기

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

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int num, index;
	vector<int> num_arr;
	cin >> num >> index;
	for (int i = 1; i <= num; ++i)
	{
		if (0 == num % i)
			num_arr.push_back(i);
	}
	if (num_arr.size() < index)
		cout << "0";
	else
		cout << num_arr.at(index - 1);
	return 0;
}
  • 간단하게 배열을 하나 만들어서 약수일 때, 그 배열에 값을 넣고 인덱스 접근으로 해결해주었다.
  • index가 size보다 크게 오는 경우가 있기 때문에 예외처리를 해주어야 한다.
728x90

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

[백준/C++] 11653 소인수분해  (0) 2024.02.20
[백준/C++] 9506 약수들의 합  (0) 2024.02.19
[백준/C++] 5086 배수와 약수  (0) 2024.02.19
[백준/C++] 2563 색종이  (0) 2024.02.19
[백준/C++] 10798 세로읽기  (0) 2024.02.19