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;
}

猜你喜欢

转载自blog.csdn.net/weixin_43935894/article/details/88828120