Algorithm/백준
[백준] 4375번 1
sirius
2021. 2. 8. 13:32
https://www.acmicpc.net/problem/4375
4375번: 1
2와 5로 나누어 떨어지지 않는 정수 n(1 ≤ n ≤ 10000)가 주어졌을 때, 1로만 이루어진 n의 배수를 찾는 프로그램을 작성하시오.
www.acmicpc.net
문제를 읽어도 무슨 소리인지 알 수가 없었다.
1, 11, 111, 1111 이런 수를 찾으라는 문제 ;;
- 1 * 10 + 1 = 11
- 11 * 10 + 1 = 111
- 111 * 10 + 1 = 1111
이 반복으로 대충 풀었지만 범위를 벗어나서 시간초과..
입력된 n으로 다시 나머지를 구해서 수를 줄이면서 반복한다.
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()) {
int n = sc.nextInt();
long num = 1;
if(n == 1) {
System.out.println(1);
break;
}
for(int i = 1; ; i++) {
num = num * 10 + 1;
num = num % n;
if(num == 0) {
System.out.println(i+1);
break;
}
}
}
}
}