c#/BAEKJOON
c# 1835번 카드 (백준)
rofn123
2023. 4. 2. 01:32
using System;
class Deque
{
private const int M = 1001;
private static int front;
private static int rear;
private static int[] data = new int[M];
public Deque() { front = rear = 0; }
public bool IsEmpty() { return front == rear; } //비었는가?
public void Fpush(int e)
{ //앞에 넣기
data[front] = e;
front = (front - 1 + 1001) % 1001;
}
public void Rpush(int e)
{ //뒤에 넣기
rear = (rear + 1) % 1001;
data[rear] = e;
}
public int Fpop()
{ //앞에서 꺼내기
front = (front + 1) % 1001;
return data[front];
}
public int Rpop()
{ //뒤에서 꺼내기
int tmp = data[rear];
rear = (rear - 1 + 1001) % 1001;
return tmp;
}
}
namespace 백준
{
public class Program
{
static void Main(String[] args)
{
Deque deq = new();
string s = Console.ReadLine();
int n = Convert.ToInt32(s);
for (int i = n; i > 0; i--)
{ //n, ... , 1까지 차례 차례 덱에 집어넣습니다.
deq.Fpush(i);
for (int j = i; j > 0; j--) deq.Fpush(deq.Rpop()); //들어간 수의 크기만큼 뒤에서 꺼내서 앞에 집어넣습니다.
//deq.display(); 덱 확인용입니다.
}
while (!deq.IsEmpty()) Console.Write(deq.Fpop()+" "); //앞에서 부터 덱에서 꺼내주면서 출력합니다
}
}
}
풀이 : deque (덱)
0. deque을 class로 구현해 줍니다.
1. n을 입력 받습니다.
2. n을 덱에 집어 넣고, 덱의 맨 뒤에서 꺼낸다음 맨 앞에다가 넣는 것을 n번하면 됩니다
3. 2의 과정은 1까지 덱에 집어넣으면, 종료합니다.
4. deque의 앞부터 출력합니다