问题 U: Snack

4 篇文章 0 订阅
2 篇文章 0 订阅

问题 U: Snack

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

Takahashi is organizing a party.
At the party, each guest will receive one or more snack pieces.
Takahashi predicts that the number of guests at this party will be A or B.
Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.
We assume that a piece cannot be divided and distributed to multiple guests.

Constraints
·1≤A,B≤105
·A≠B
·All values in input are integers.

输入

Input is given from Standard Input in the following format:

A B

输出

Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.

样例输入 Copy

【样例1】
2 3
【样例2】
123 456
【样例3】
100000 99999

样例输出 Copy

【样例1】
6
【样例2】
18696
【样例3】
9999900000

提示

样例1解释
When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.

思路分析:要满足两个数都符合最小的平均数的意思就是找出这两个数的最小公倍数。

                       故只需要使用寻找两个数的最小公倍数的算法即可!!!

附上寻找最小公倍数的链接:

C语言求两个正整数的最小公倍数_-SwaggyP的博客-CSDN博客_c语言求两个正整数的最小公倍数

AC代码:

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int lcm4(int m,int n)
{
    while(m!=n)
	{
		if(m>n)
		{
			m=m-n;
		}else
		{
			n=n-m;
		}	
	}
	return n;
}

int main(int argc, char** argv) {
	long long int a,b;
	cin>>a>>b;
	cout<<a*b/lcm4(a,b);
	return 0;
}

如有更好思路,请指教!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值