• 문제 링크
11322번: Numbers are Easy
For each test case, output the smallest positive number X such that X is divisible by N and it contains only digits ’0’ and ’1’. Given the constraints, it is guaranteed that there always is a solution (for any positive N) and, in this problem, it w
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int t = read(), n;
while (t-- > 0) sb.append((n = read()) == 1 ? 1 : bfs(n)).append("\n");
bw.write(sb.toString());
bw.flush();
}
private static long bfs(int n) {
Queue<Long> q = new ArrayDeque<>();
q.offer(1L);
while (!q.isEmpty()) {
long x = q.poll() * 10;
if (x % n == 0) return x;
q.offer(x);
if (++x % n == 0) return x;
q.offer(x);
}
return -1;
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 21313 문어 - Greedy / Java (0) | 2023.11.19 |
---|---|
[백준] 6125 Treasure Cave - Graph Theory / Java (0) | 2023.11.17 |
[백준] 11448 Ga - Graph Theory / Java (0) | 2023.11.15 |
[백준] 6004 The Chivalrous Cow - Graph Theory / Java (0) | 2023.11.14 |
[백준] 26876 New Time - Graph Theory / Java (0) | 2023.11.13 |
댓글