Baekjoon Algorithm | Problem 4375: 1

Source

https://www.acmicpc.net/problem/4375

Problem

Solve Baekjoon Online Judge problem 4375, 1.

Input

Follow the input format and constraints given in the original problem statement.

Output

Print the answer required by the problem.

Sample Input 1

3 7 9901

Sample Output 1

3 6 12

Algorithm Classification

  • Math
  • Brute Force
  • Number Theory

Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            String str = null;
            while ((str = br.readLine()) != null) {
                final int n = Integer.parseInt(str);

                int x =1;
                for (int i = 1; ; i++) {
                    x = x % n;
                    x = x * 10 + 1;
                    if (x == 1) {
                        System.out.println(i);
                        break;
                    }
                }
            }
        }
    }
}