forked from EileenFeng/240FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetSteps.java
29 lines (27 loc) · 810 Bytes
/
GetSteps.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
import java.util.*;
import java.io.*;
class GetSteps{
public static ArrayList<String> operations = new ArrayList<String>();
public static void main(String[] args){
if(args.length < 2) {
System.out.println("Please enter a number");
} else {
int result = getsteps(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
System.out.println("result is " + result);
}
}
public static int getsteps(int input, int target) {
if(input == target || input <= 0) {
return 0;
}
if(input % 2 == 0) {
if(input % 3 == 0) {
return 1 + Math.min(getsteps(input-1, target), Math.min(getsteps(input/2, target), getsteps(input/3, target)));
}else{
return 1 + Math.min(getsteps(input-1, target), getsteps(input/2, target));
}
} else {
return 1 + getsteps(input-1, target);
}
}
}