C# 2164번 카드2
2022. 2. 15. 23:32ㆍ코딩테스트 문제 풀이/[백준] 큐, 덱
728x90
반응형

내 코드 :
using System;
using System.Collections.Generic;
namespace _2
{
class Program
{
static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine());
Queue<int> q = new Queue<int>();
//카드를 버릴건지 밑으로 옮길건지
//확인하기 위한 bool
bool cardOut = false;
int card = 0;
for (int i = 1; i <= N; i++)
{
q.Enqueue(i);
}
while(q.Count > 1)
{
if(!cardOut)
{
q.Dequeue();
cardOut = true;
}
else
{
card = q.Dequeue();
q.Enqueue(card);
cardOut = false;
}
}
Console.WriteLine(q.Dequeue());
}
}
}
풀이 설명 :
1. N만큼 1부터 N까지 스택에 쌓는다.
2. 맨 위에 카드를 버린다.
3. 맨 위의 카드를 맨 아래로 내린다.
4. 큐의 길이가 1이 되면 2,3의 과정을 멈추고 큐의 마지막 값을 출력한다.
728x90
반응형
'코딩테스트 문제 풀이 > [백준] 큐, 덱' 카테고리의 다른 글
C# 1966번 프린터 큐 (0) | 2022.02.16 |
---|---|
C# 11866번 요세푸스 문제 0 (0) | 2022.02.15 |
C# 18258번 큐 2 (0) | 2022.02.15 |