- std::list로 값 관리
-> 2부터 N까지 모든 정수를 담는다.
- while
-> 가장 작은수는 list의 맨앞의 값 (P)
-> P와 P의 개수값 제거해가며 K번째 지우는 수를 찾는다.
#include <iostream>
#include <list>
int main()
{
std::list<int> nums;
int N, K;
std::cin >> N >> K;
for (int i = 2; i <= N; i++)
{
nums.push_back(i);
}
int checkcount = 0;
while (true)
{
int CalValue = nums.front();
nums.pop_front();
++checkcount;
if (checkcount == K)
{
std::cout << CalValue<<"\n";
break;
}
std::list<int>::iterator StartIter = nums.begin();
std::list<int>::iterator EndIter = nums.end();
for (; StartIter != EndIter; ++StartIter)
{
int deleteValue = *StartIter;
if (deleteValue % CalValue == 0)
{
*StartIter = -1;
++checkcount;
if (checkcount == K)
{
std::cout << deleteValue << "\n";
return 0;
}
}
}
while (true)
{
if (nums.front() != -1)
{
break;
}
nums.pop_front();
}
}
return 0;
}
'C++ > 백준 C++' 카테고리의 다른 글
[백준]C++ 1213 팰린드롬 만들기 [구현, 문자열] (0) | 2024.11.10 |
---|---|
[백준]C++ 2161 카드1 [구현] (2) | 2024.11.09 |
C++ 2167 2차원 배열의 합 [구현, 누적합] (0) | 2024.11.07 |
C++ 1009 분산 처리 [구현] (0) | 2024.11.06 |
C++ 1475 방 번호 [구현] (0) | 2024.11.05 |