描述:
标题:6是最小的,1到3所有数的倍数。(6 = 1 * 6 = 2 * 3 = 3 * 2)2520是最小的,1到10的所有数字的倍数。
输入n,输出最小的正整数,他是1到n所有数的倍数。
Input
输入第一行组数T, 接下来T行,每行一个整数n。 (1 <= T <= 20) (1 <= N <= 20)
Output
对于每组数据,输出一个数,表示1到n的最小公倍数。
Sample Input
3
3
10
20
Sample Output
6
2520
232792560
#include <iostream>
#include<vector>
#include<string.h>
using namespace std;
const int MOD = 1e9+7;
const int MAX = 1e5+5;
long long gys(long long a,long long b)
{
long long c=1,x,y;
x=a>b?a:b;
y=a<b?a:b;
while(c){
c=x%y;
x=y;
y=c;
}
return x;
}
int main()
{
int t,n;
long long a,b;
cin>>t;
while(t--){
cin>>n;
a=1;b=2;
while(n>1&&b<=n){
a=a*b/gys(a,b);
b++;
}
cout<<a<<endl;
}
return 0;
}
本文介绍了一种求解从1到n所有整数最小公倍数的算法,通过不断迭代计算并使用辗转相除法求最大公约数,进而求得最小公倍数。示例代码展示了如何处理多组测试数据,并提供了样例输入输出。
1959

被折叠的 条评论
为什么被折叠?



