https://www.acmicpc.net/problem/1158
์ค๋ต๋ ธํธ(ํธ๋ ๋์ ์ฌ๊ณ ๊ณผ์ ๋ฐ ๊ฐ์ ์ )
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
Queue<Integer> q = new LinkedList<>();
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
for(int i=1; i<=n; i++){
q.offer(i); // 1๋ถํฐ N๊น์ง
}
System.out.print("<");
while(!q.isEmpty()){
// k-1๋งํผ ๋ค๋ก ๋ณด๋ด๊ณ , poll
for(int i=0; i<k-1; i++){
q.offer(q.poll());
}
System.out.print(q.poll()+", ");
if(q.size()==1){
System.out.print(q.poll());
}
}
System.out.print(">");
}
}
ํ๋ฒ์ ๋ง์ถ ์ ์์๋ ๋ฐ ์ ๋ง ์์ฌ์ด ๋ฌธ์ ์๋ค. ์์ด๋์ด๋ ํ๋ฅผ ์ฌ์ฉํด์, pollํ๊ฑธ ๊ณง๋ฐ๋ก offerํด์ฃผ๋, ์ํ ํ ๋ฐฉ์์ ์ฌ์ฉํ๋ค. 95%์ฏค์์ ํ๋ ค์ ์ง๋ฌธ๊ฒ์ํ์ ๋ฐ๋ก๋ฅผ ๊ณ์ ์บ๋ณด๋ค ๋ณด๋ 1 1 ์ผ ๋ ๊ฐ์ด ์ด์ํ๊ฒ ๋์จ๋ค๋ ๊ฑธ ์๊ฒ ๋๋ค. 1 1์ด๋ฉด if๋ฌธ์ผ๋ก ์ ์ธํ ๋ถ๋ถ๋ง ํ๊ฒ ๋์ด์ <1, >๋ก ์ถ๋ ฅ์ด ๋๋ค. ์ฌ๋ฐ๋ฅธ ๋ต์ <1>์ด๋ค.
์ ๋ต์ฝ๋
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
Queue<Integer> q = new LinkedList<>();
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
for(int i=1; i<=n; i++){
q.offer(i); // 1๋ถํฐ N๊น์ง
}
System.out.print("<");
while(!q.isEmpty()){
if(q.size()==1){
System.out.print(q.poll());
}else{
// k-1๋งํผ ๋ค๋ก ๋ณด๋ด๊ณ , poll
for(int i=0; i<k-1; i++){
q.offer(q.poll());
}
System.out.print(q.poll()+", ");
}
}
System.out.print(">");
}
}
๋์์ด ๋๋ค๋ฉด ๋๊ธ์ด๋ ๊ณต๊ฐ ๋ฒํผ ํ ๋ฒ์ฉ ๋๋ฅด๊ณ ๊ฐ์ฃผ์ธ์! ๋ก๊ทธ์ธ ์ํด๋ ๋ฉ๋๋ค ^_^
๋ฐ์ํ
'OJ๐ผ > ์ค๋ต๋ ธํธ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ][JAVA] 11068: ํ๋ฌธ์ธ ์ (0) | 2024.01.14 |
---|---|
[BOJ][JAVA] 1260 : DFS์ BFS (0) | 2024.01.13 |
[BOJ][JAVA] 10814: ๋์ด์ ์ ๋ ฌ (0) | 2024.01.09 |
[BOJ][JAVA] 10816: ์ซ์ ์นด๋ 2 (0) | 2024.01.08 |
[BOJ][JAVA] 2295: ์ธ ์ ์ฐพ๊ธฐ (0) | 2024.01.07 |