C++/백준 C++

[백준]C++ 2161 카드1 [구현]

CE : 하랑 2024. 11. 9. 21:12

 

 

 

 

-> std::list<int> 로 1~N 까지의 카드를 순서대로 관리

-> While

- 첫번째 카드 버리고

- 그 다음 카드는 맨 뒤로 보내는걸 카드 한장이 남을때까지 반복

 

#include <iostream>
#include <list>

int main()
{
	std::list<int> Cards;

	int N;
	
	std::cin >> N;

	for (int i = 1; i <= N; i++)
	{
		Cards.push_back(i);
	}

	while (Cards.size()!=1)
	{
		std::cout << Cards.front() << " ";

		Cards.pop_front();

		int frontCard = Cards.front();

		Cards.pop_front();

		Cards.push_back(frontCard);
	}

	std::cout << Cards.front() << "\n";

	return 0;
}