hdu 1019解题报告

来源:互联网 发布:nginx负载均衡方式 编辑:程序博客网 时间:2024/05/03 01:10

一道简单的数学题,先利用欧几里得算出最大公约数,然后利用他们间的关系,例如12 ,8的最大公约数是4,最小公倍数=12*(8/4)=24;

source code:

 

#include <iostream>
using namespace std;
int a[1005];

int GCD(int a, int b)
{
 int temp;
 if (a<b)
 {
  temp=a;
  a=b;
  b=temp;     
 }
 //swap(a,b)
 if (b==0)
  {
   return a;       
  }  
  else
   {
    return GCD(b,a%b);     
   }
}


int LCM(int a, int b)
{
 return (a*(b/GCD(a,b)));  
}


int main()
{
 int i,times,numbers,results;  
 cin>>times;
 while (times--)
  {
   cin>>numbers;
   for (i=0;i<numbers;i++)
    {
     scanf("%d",&a[i]);                    
    }
    if (numbers==1)
    {
     cout<<a[0]<<endl;
    }
    else
    {
    results=LCM(a[0],a[1]);
    for (i=2;i<numbers;i++)
     {    
      results=LCM(results,a[i]);                    
     }
   cout<<results<<endl;     
  }
}
  return 0;
}

 

原创粉丝点击