SOLUTION INFO
C++ · main.cpp
- 작성자
- tony9402
- 공동 작성자
- 없음
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N; cin >> N;
queue<int> Q;
for(int i=1;i<=N;i++) Q.push(i);
while((int)Q.size() > 1) {
Q.pop();
Q.push(Q.front());
Q.pop();
}
cout << Q.front();
return 0;
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Java · Main.java
- 작성자
- semInDev
- 공동 작성자
- 없음
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args)throws IOException{
FastReader input = new FastReader();
int N = input.nextInt();
Queue<Integer> queue = new ArrayDeque<>();
for(int i=1; i<N+1; i++) { // queue 초기화
queue.add(i);
}
while(queue.size() != 1) {
queue.poll(); // 맨 위 카드 하나 버리기
int temp = queue.poll(); // 맨 위 카드 하나 아래로 넣기
queue.add(temp);
}
System.out.println(queue.poll());
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() throws IOException{
br = new BufferedReader(new InputStreamReader(System.in)); //백준 제출 시 주석해제를 하면 됩니다.
// br = new BufferedReader(new FileReader("input.txt")); //IDE 실행 시 주석해제를 하면 됩니다.
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
- java 컬렉션 프레임워크의 Queue(ArrayDeque로 구현)를 사용한다.
쌓아놓은 카드의 윗부분을 큐의 front 쪽, 아래를 back 쪽이라고 둔다.
SOLUTION INFO
Python · main.py
- 작성자
- gusdn3477
- 공동 작성자
- 없음
import sys
from collections import deque
def input():
return sys.stdin.readline().rstrip()
queue = deque()
N = int(input())
for i in range(1,N+1):
queue.append(i)
while True:
a = queue.popleft()
if not queue:
print(a)
break
b = queue.popleft()
queue.append(b)
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.