TOJ 10009 GCD and LCM

题目链接 : TOJ10009


 

10009 - GCD and LCM

Time Limit: 1000MS
Memory Limit: 65536KB

Description
In mathematics, the greatest common divisor(GCD), sometimes known as the greatest common factor (GCF) or highest common factor (HCF), of two integers, is the largest positive integer that divides both numbers without remainder. The least common multiple(LCM) or lowest common multiple (LCM) or smallest common multiple(SCM) of two integers a and b is the smallest positive integer that is a multiple of both a and b.

In this problem, we define the GCD of an integer series as the largest positive integer that divides every number of that series without remainder. And the LCM of an integer series as the smallest positive integer that is a multiple of every number of the series.

For example, for the following integer series:

8 16 12 24

We have GCD = 4 and LCM = 48.

You are given an non-negative integer series, please calculate its GCD and LCM.


Input
The input will consist of a number of non-negative integers, one per line. Proceed to the end of input.



Output
Two integers, the series' GCD and LCM, separated by a single space. Our input guarantees the correct answer will never exceed 263-1.



Sample Input
8
16
12
24



Sample Output
4 48

 

欧几里得算法

 

#include<cstdio>


long long gcd(long long a,long long b)
{
	long long c=a%b;
	while(c)
	{
		a=b;b=c;
		c=a%b;
	}
	return b;
}
int main()
{
	long long ans1=0,ans2=0,k,temp;
	while(scanf("%lld",&temp)==1)
	{
		if(ans1==0) 
		{
			ans1=temp;
			ans2=temp;
		}
		else 
		{
			ans1=gcd(ans1,temp);
			ans2=ans2/gcd(ans2,temp)*temp;
		}
	}
	printf("%lld %lld",ans1,ans2);
	return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值