forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6.java
30 lines (24 loc) Β· 833 Bytes
/
6.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// N, Kλ₯Ό 곡백μ κΈ°μ€μΌλ‘ ꡬλΆνμ¬ μ
λ ₯ λ°κΈ°
int n = sc.nextInt();
int k = sc.nextInt();
int result = 0;
while (true) {
// Nμ΄ Kλ‘ λλμ΄ λ¨μ΄μ§λ μκ° λ λκΉμ§λ§ 1μ© λΉΌκΈ°
int target = (n / k) * k;
result += (n - target);
n = target;
// Nμ΄ Kλ³΄λ€ μμ λ (λ μ΄μ λλ μ μμ λ) λ°λ³΅λ¬Έ νμΆ
if (n < k) break;
// Kλ‘ λλκΈ°
result += 1;
n /= k;
}
// λ§μ§λ§μΌλ‘ λ¨μ μμ λνμ¬ 1μ© λΉΌκΈ°
result += (n - 1);
System.out.println(result);
}
}