Description

The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.        
                

Input

Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.        
                

Output

For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.        
                

Sample Input

2
3 5 7 15
6 4 10296 936 1287 792 1
                

Sample Output

105
10296
 
 
如下为一般方法写的代码
#include<iostream>
using namespace std;
int s[];
__int64 x;
int cmp ( const void *a , const void *b )
{
return *(int *)b - *(int *)a;
}
void f(int s[],int n)
{
int p=;
bool flag;
while(){
flag=true;
x=s[]*p;
for(int i=;i<n;i++){
if(x%s[i]!=){
flag=false;
break;
}
}
if(flag==true)break;
p++;
}
cout<<x<<endl;
}
int main()
{
int n;
cin>>n;
while(n--){
int a;
cin>>a;
for(int i=;i<a;i++)cin>>s[i];
qsort(s,a,sizeof(s[]),cmp);
f(s,a);
}
return ;
}

如下为运用辗转相除法写的代码

#include<iostream>
using namespace std;
int s[];
__int64 x,y;
int g(int a,int b)
{
int i,t;
y=a*b;
if(a<b){
t=a;
a=b;
b=t;
}
while(b){
t=b;
b=a%b;
a=t;
}
return y/a;
}
void f(int s[],int n)
{
x=s[];
for(int i=;i<n;i++){
x=g(s[i],x);
}
cout<<x<<endl;
}
int main()
{
int n;
cin>>n;
while(n--){
int a;
cin>>a;
for(int i=;i<a;i++)cin>>s[i];
f(s,a);
}
//system("pause");
return ;
}

L - 辗转相除法(第二季水)的更多相关文章

  1. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  2. D - Counterfeit Dollar(第二季水)

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  3. N - Robot Motion(第二季水)

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  4. S - 骨牌铺方格(第二季水)

    Description          在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种, ...

  5. R - 一只小蜜蜂...(第二季水)

    Description          有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数.         其中,蜂房的结构如下所示.     ...

  6. I - Long Distance Racing(第二季水)

    Description Bessie is training for her next race by running on a path that includes hills so that sh ...

  7. Y - Design T-Shirt(第二季水)

    Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA ...

  8. B - Maya Calendar(第二季水)

    Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old ...

  9. T - 阿牛的EOF牛肉串(第二季水)

    Description          今年的ACM暑期集训队一共有18人,分为6支队伍.其中有一个叫做EOF的队伍,由04级的阿牛.XC以及05级的COY组成.在共同的集训生活中,大家建立了深厚的 ...

随机推荐

  1. 13年山东省赛 Mountain Subsequences(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mountain Subsequences Time Limit: 1 Sec   ...

  2. 关于"zoom“ 的一点小认识

    最早接触zoom是在清除浮动的时候,原因就是zoom能触发IE的haslayout,当时也没深究其原理,今天,在查看张鑫旭的对overflow与zoom”清除浮动”的一些认识时,其中提到zoom是比例 ...

  3. git操作流程

  4. mysql按月,按日分组统计数据

    group by DATE_FORMAT(createtime,'%Y-%m');//按月统计数据 group by DATE_FORMAT(createtime,'%Y-%m-%d');//按天统计 ...

  5. $_FILES系统函数

    PHP编程语言中的常见的$_FILES系统函数用法有: $_FILES['myFile']['name'] 显示客户端文件的原名称. $_FILES['myFile']['type'] 文件的 MIM ...

  6. [原]此程序专用来说明C++模板的用法

    #include using namespace std; //此程序专用来说明模版的使用 template void swap1(T& a,T& b){     T temp=a; ...

  7. <Programming Collective Intelligence> Chapter2:Making Recommendations

    <Programming Collective Intelligence> Chapter2:Making Recommendations 欧几里得距离评价 皮尔逊相关度评价 它相比于欧几 ...

  8. PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\\Users\\video\\AppData\\Local\\Temp\\tmpfipzk8ma'--问题解决

    学习python+selenium总是遇到各种问题 代码如下: from selenium import webdriverbrowser = webdriver.PhantomJS() #浏览器初始 ...

  9. SQL Server索引 - 聚集索引、非聚集索引、非聚集唯一索引 <第八篇>

    聚集索引.非聚集索引.非聚集唯一索引 我们都知道建立适当的索引能够提高查询速度,优化查询.先说明一下,无论是聚集索引还是非聚集索引都是B树结构. 聚集索引默认与主键相匹配,在设置主键时,SQL Ser ...

  10. vc++ ODBC

    http://book.51cto.com/art/200807/80054.htm http://www.cnblogs.com/xrong/archive/2013/04/14/3020240.h ...