hdoj 2028 Lowest Common Multiple Plus (最小公倍数,最大公约数)

Lowest Common Multiple Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49546    Accepted Submission(s): 20479


Problem Description
求n个数的最小公倍数。
 

Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
 

Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
 

Sample Input
   
   
   
   
2 4 6 3 2 5 7
 

Sample Output
   
   
   
   
12 70
代码:
#include <iostream>
#include <cstdio>
using namespace std;

int f(int a,int b)
{
    int m;
    int t;
    int ta=a,tb=b;
    if(a<b)
    {
        t=a;a=b;b=t;
    }
    while(b)
    {
        m=a%b;
        a=b;
        b=m;
    }
    return ta/a*tb;//返回最大公倍数
}
int main()
{
    long long sum;
    int n,a[110];
    while(~scanf("%d",&n))
    {
        sum=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum=f(sum,a[i]);
        }
        printf("%lld\n",sum);
    }
    return 0;
}


#include <cstdio>
int f(int a,int b)

{
    int t;
    int m;
    if(a<b)
    {
        t=a;a=b;b=t;
    }
    while(b)
    {
        m=a%b;
        a=b;
        b=m;
    }
    return a;//返回最大公约数
}
int main()
{
    int m;
    int n,a[110];
    while(~scanf("%d",&n))
    {
        scanf("%d",&a[0]);
        for(int i=1;i<n;i++)
        {
            scanf("%d",&a[i]);
            m=f(a[i-1],a[i]);
            a[i]=a[i-1]/m*a[i];
        }
        printf("%d\n",a[n-1]);
    }
    return 0;
}


你可能感兴趣的:(hdoj 2028 Lowest Common Multiple Plus (最小公倍数,最大公约数))