HDU:2028 lowest common multiple plus

必須滾瓜爛熟求最大公倍數和最小公約數

Problem Description

求n個數的最小公倍數。


Input
輸入包含多個測試實例,每個測試實例的開始是一個正整數n,然後是n個正整數。


Output
爲每組測試數據輸出它們的最小公倍數,每個測試實例的輸出佔一行。你可以假設最後的輸出是一個32位的整數。


Sample Input
2 4 6
3 2 5 7


Sample Output
12

70

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int lcd(int a,int b)
{
    int k=a;
    while(k%b!=0){
        k+=a;
    }
    return k;
}
int main()
{
    int n,i,j;
    int a[120];
    while(scanf("%d",&n)!=EOF){
        for(i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        j=lcd(a[0],a[1]);
        for(i=2;i<n;i++){
            j=lcd(j,a[i]);
        }
        printf("%d\n",j);
    }

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章