Multiple Clocks(gcd和lcm)

题目描述
We have N clocks. The hand of the i-th clock (1≤i≤N) rotates through 360° in exactly Ti seconds.
Initially, the hand of every clock stands still, pointing directly upward.
Now, Dolphin starts all the clocks simultaneously.
In how many seconds will the hand of every clock point directly upward again?

Constraints
1≤N≤100
1≤Ti≤1018
All input values are integers.
The correct answer is at most 1018 seconds.

输入
Input is given from Standard Input in the following format:
N
T1
:
TN

输出
Print the number of seconds after which the hand of every clock point directly upward again.

样例输入
2
2
3

样例输出
6

提示
We have two clocks. The time when the hand of each clock points upward is as follows:
·Clock 1: 2, 4, 6, … seconds after the beginning
·Clock 2: 3, 6, 9, … seconds after the beginning
Therefore, it takes 6 seconds until the hands of both clocks point directly upward.

思路:
求最小公倍数,注意先除后乘

代码实现

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
using namespace std;
typedef long long ll;
const int N=105;
const ll mod=192600817;
int n;
ll T[N];
ll lc=1;
ll gcd(ll a,ll b)
{
    if(b>a) return gcd(b,a);
    if(b==0) return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    if(a%b==0) return a;
    return a/gcd(a,b)*b; //注意先除后乘
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&T[i]);
        lc=lcm(lc,T[i]);
    }
    printf("%lld\n",lc);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值