问题描述
小张是软件项目经理,他带领3个开发组。工期紧,今天都在加班呢。为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑)。他的要求是:
-
各组的核桃数量必须相同
-
各组内必须能平分核桃(当然是不能打碎的)
-
尽量提供满足1,2条件的最小数量(节约闹革命嘛)
输入格式
输入包含三个正整数a, b, c,表示每个组正在加班的人数,用空格分开(a,b,c<30)
输出格式
输出一个正整数,表示每袋核桃的数量。
样例输入1
2 4 5
样例输出1
20
样例输入2
3 1 1
样例输出2
3
import java.util.Scanner;
public class Main{
public static int gy(int a,int b) {
return a%b==0?b:gy(b, a%b); //用此方法求最大公约数
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int m = 0;
m = a*b/gy(a, b); //最小公倍数就等于 两者相乘除以最大公约数
System.out.println(m*c/gy(m,c));
}
}
暴力解法:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println(cal(a, b, c));
}
public static int cal(int a,int b,int c) {
int i = 1;
while(i<=a*b*c) {
if(i%a==0&&i%b==0&&i%c==0) {
return i;
}
i++;
}
return 0;
}
}