題目描述
給定5個正整數, 它們的最小的衆倍數是指的能夠被其中至少三個數整除的最小正整數。 給定5個不同的正整數, 請計算輸出它們的最小衆倍數。
輸入描述:
輸入包括一行,一行中有五個各不相同的正整數a, b, c, d, e(1 ≤ a, b, c, d, e ≤ 100), 以空格分割
輸出描述:
輸出一個整數,表示它們的最小衆倍數
示例1
輸入
1 2 3 4 5
輸出
4
數據小,直接遍歷
#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int x, int y)
{
if(x < y)
swap(x, y);
return y == 0? x : gcd(y, x % y);
}
int lcm(int x, int y)
{
int z = gcd(x, y);
return x * y / z;
}
int main()
{
int *arr = new int(5);
for(int i = 0; i < 5; ++i)
cin >> arr[i];
int ans = 1e6 + 1;
for(int i = 0; i < 5; i++)
{
for(int j = i + 1; j < 5; j++)
{
for(int k = j + 1; k < 5; k++)
{
ans = min(ans, lcm(lcm(arr[i], arr[j]), arr[k]));
}
}
}
cout << ans << endl;
return 0;
}