#include <iostream>
#include <list>
using namespace std;
int main(int argc, char const *argv[])
{
cin.tie(NULL);
ios::sync_with_stdio(false);
int n;
cin >> n;
list<int> lt;
for (int i = 1; i <= n; i++)
{
lt.push_back(i);
}
while (lt.size() > 1)
{
lt.pop_front();
lt.push_back(lt.front());
lt.pop_front();
}
cout << lt.front() << '\n';
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main()
{
cin.tie(NULL);
ios::sync_with_stdio(false);
int n;
cin >> n;
queue<int> q;
for (int i = 1; i <= n; i++)
{
q.push(i);
}
while (q.size() > 1)
{
q.pop();
q.push(q.front());
q.pop();
}
cout << q.front() << '\n';
return 0;
}
처음 시도했던 코드
while (!lt.empty())
{
lt.pop_front();
int temp = lt.front();
if (lt.size() == 1)
{
cout << temp << endl;
break;
}
lt.pop_front();
lt.push_back(temp);
}