Problem Statement- Harsh just loves numbers and loves to have fun with them . Apparently he has two numbers with him , say X and Y . Both of them are integers . Now from these numbers he wants another number , say L . But he doesn’t know how to derive it and needs your help . Given the two numbers you can add one to another , and you can do this any number of times . For example, you can add X to Y , or Y to X , and this can be done until any combination makes either X equal toL or Y equal to L .
“Well , this might actually lead to a solution”, said Harsh, ”but can you do it in a minimum number of operations ?”.
Just to make this problem a little simple let’s assume both the values of X and Y is 1 .
Input Format:
The first line contains T, the number of test cases. For each test case, the first line contains L ,the value that Harsh wants .
Output Format:
For each test case, output the required minimum number of operations .
Problem Link- ( https://www.hackerearth.com/problem/algorithm/simple-addition )
here is the code ....
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class TestClass {
static int evaluateStrings(int a, int b) {
if (b == 0 && a != 1) return 100000000;
if (b == 0 && a == 1) return -1;
else return (a / b + evaluateStrings(b, a % b));
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
for (int i = 1; i <= N; i++) {
int x = Integer.parseInt(br.readLine());
int ans = 100000000;
for (int j = 1; j <= x; j++) {
int tmp = evaluateStrings(x, j);
if (tmp < ans) ans = tmp;
}
System.out.println(ans);
}
}
}
No comments:
Post a Comment