Lowest Common Multiple Plus(最小公倍数)

来源:互联网 发布:数据库round down函数 编辑:程序博客网 时间:2024/05/21 20:22

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
 int  n;
 __int64 x,y,a,b,t;
 while(scanf("%d",&n)!=EOF)
 {
       scanf("%I64d",&x);
       a=x;
       while(--n)
       {
         scanf("%I64d",&y);
         b=y;
         while(y)
         {
          t=x%y;
          x=y;
          y=t;
         }
         x=a*b/x;
         a=x;
       }
       printf("%I64d\n",x);
 }
 return 0;
}

 

#include<stdio.h>

typedef unsigned long UL;

UL gcd(UL u, UL v)

{    int remainder; 

       remainder = u % v; 

   while(remainder)   

 {        u = v;   

     v = remainder;    

    remainder = u % v;   

 }  

  return v;

}

UL lcm(UL u, UL v)

{   

 return u * v / gcd(u, v);

}

int main(void)

{    int n;   

UL u;  

  UL res;  

      while (scanf("%d", &n) != EOF

      {     

       res = 1;      

       while (n--)    

        {       

            scanf("%lu", &u);     

            res = lcm(res, u);       

       }     

          printf("%lu\n", res);   

     }   

  return 0;

原创粉丝点击