hdu 1019 Least Common Multiple

来源:互联网 发布:mac完全用户手册 编辑:程序博客网 时间:2024/04/30 03:31

题目简单描述

求一组数的最小公倍数

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1019

由于题目比较简答,这里仅仅对样例进行简单的说明

/** * * 2 两组测试样例 * 3 5 7 15 第一组 有三个数  分别是5 7 15  最小公倍数是105 * 6 4 10296 936 1287 792 1  第二组有六个数 分别是 4 10296 936 1287 792 1 最小公倍数是 10296 */

下边直接上代码了

#include <stdio.h>int a[1005];/** *   辗转相除法 *   递归求两个数的最大公约数 * *  @return 返回两个数的最大公公约数 */int GCD(int a, int b){    return a%b?GCD(b,a%b):b ;}/** *  求两个数的最小公倍数 * *  @return 最小公倍数 */int LCM(int a, int b){    return (a*(b/GCD(a,b)));}int main(){    int i,times,numbers,answer;    scanf("%d",×);    while (times--)    {                scanf("%d",&numbers);        for (i=0;i<numbers;i++)        {            scanf("%d",&a[i]);        }        if (numbers==1)        {            printf("%d\n",a[0]);        }        else        {            answer=LCM(a[0],a[1]);            for (i=2;i<numbers;i++)            {                     answer=LCM(answer,a[i]);            }            printf("%d\n",answer);        }    }    return 0;}


0 0