-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedy2.java
More file actions
48 lines (42 loc) · 1.38 KB
/
Copy pathGreedy2.java
File metadata and controls
48 lines (42 loc) · 1.38 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package dataStructure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Greedy2 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int result = 0;
while (true) {
// n이 k로 나누어 떨어지는 수가 될 때까지 빼기
int target = (n / k) * k;
result += (n - target);
n = target;
// n이 k보다 작을 때 (더 이상 나눌 수 없을 때) 반복문 탈출
if (n < k) break;
// k로 나누기
result += 1;
n /= k;
}
result += (n - 1);
System.out.println(result);
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// StringTokenizer st = new StringTokenizer(br.readLine(), " ");
// int N = Integer.parseInt(st.nextToken());
// int K = Integer.parseInt(st.nextToken());
// int count = 0;
// while (N != 1) {
// if (N % K == 0) {
// N = N / K;
// count++;
// } else {
// N--;
// count++;
// }
// }
// System.out.println(count);
}
}