数论:zoj 1797 Least Common Multiple

本文介绍了一个使用C++编写的程序,用于计算一组整数的最大公约数。程序通过逐步减小两个数并求余的方式找到最大公约数,并在处理多个数时进行迭代计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【转】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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值