【转】http://blog.youkuaiyun.com/zxy_snow/article/details/6011166
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <memory.h>
#define MAX 10001
using namespace std;
long long int gcd(long long int x,long long int y)
{
return y == 0? x : gcd(y,x%y);
}
int main(void)
{
int num[MAX];
int ncases,n;
scanf("%d",&ncases);
while( ncases -- )
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&num[i]);
if( n == 1 )
{
printf("%d/n",num[0]);
continue;
}
int temp = gcd(num[0],num[1]);
if(temp == 0 )
{
printf("%d/n",temp);
continue;
}
temp = num[0]/temp*num[1];
for(int i=2; i<n; i++)
{
int x = gcd(temp,num[i]);
if( x == 0 )
{
temp = 0;
break;
}
temp = temp/x*num[i];
}
printf("%d/n",temp);
}
return 0;
}
本文介绍了一个使用C++编写的程序,用于计算一组整数的最大公约数。程序通过逐步减小两个数并求余的方式找到最大公约数,并在处理多个数时进行迭代计算。
883

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



