试题 历届试题 核桃的数量 java题解

资源限制
时间限制:1.0s   内存限制:256.0MB

问题描述

小张是软件项目经理,他带领3个开发组。工期紧,今天都在加班呢。为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑)。他的要求是:

1. 各组的核桃数量必须相同

2. 各组内必须能平分核桃(当然是不能打碎的)

3. 尽量提供满足1,2条件的最小数量(节约闹革命嘛)


 

输入格式
输入包含三个正整数a, b, c,表示每个组正在加班的人数,用空格分开(a,b,c<30)
输出格式
输出一个正整数,表示每袋核桃的数量。

样例输入1
2 4 5
样例输出1
20

样例输入2
3 1 1
样例输出2
3

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String args[]){
 5         Scanner in=new Scanner(System.in);
 6         int a,b,c;
 7         a=in.nextInt();b=in.nextInt();c=in.nextInt();
 8         int d=lcm(a,b);
 9         int ans=lcm(c,d);
10         System.out.println(ans);
11     }
12     public static int gcd(int a,int b){
13         int temp;
14         if (a<b){
15             temp=a;
16             a=b;
17             b=temp;
18         }
19         while (b!=0){
20             temp=a%b;
21             a=b;
22             b=temp;
23         }
24         return a;
25     }
26 
27     public static int lcm(int a,int b){
28         int temp_lcm;
29         temp_lcm=a*b/gcd(a,b);
30         return temp_lcm;
31     }
32 }

题解:因为是三个数,所以先调用前面的两个数的最小公倍数(d) 再调方法求出d和最后一个(c)的最小公倍数。

方法:gcd(int a,int b) => 使用辗除法求出最小公约数 

方法:lcm(int a,int b) => 求公倍数

posted @ 2021-04-09 11:17  贾祥啊  阅读(88)  评论(0)    收藏  举报