Skip to content

Instantly share code, notes, and snippets.

@bahriddin
Created November 2, 2016 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bahriddin/f551fdc36b189cc231c552fe3beb05fa to your computer and use it in GitHub Desktop.
Save bahriddin/f551fdc36b189cc231c552fe3beb05fa to your computer and use it in GitHub Desktop.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
int a, b, d, tmp, res;
for (int i = 0; i < q; i++) {
a = in.nextInt();
b = in.nextInt();
d = in.nextInt();
if(a > b) {
tmp = a;
a = b;
b = tmp;
}
res = d + b;
tmp = d / b + 1;
if(d % b == 0)
res = d / b;
else if(d % a == 0) {
res = d / a;
if(tmp < res)
res = tmp;
}
else if(d < b)
res = 2;
else {
for (int j = d / b; j > 0; j--) {
if((d - b*j) % a == 0) {
res = j + (d - b * j) / a;
break;
}
}
if(tmp < res)
res = tmp;
}
System.out.println(res);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment