题目:Lowest common multiple plus

代码:

#include<stdio.h>
int common(int a,int b)//计算最大公约数
{
int c=a%b,t=0;
if(b>a)
{
t=b;
b=a;
a=t;
}
while(a%b!=0)
{
c=a%b;
a=b;
b=c;
}
return b;
}
int q[105];
int main()
{
int n,i,j,t=0; while(scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&q[i]);
} t=q[1]; //printf("%d\n",common(4,6)); for(j=2;j<=n;j++)
{
t=q[j]*t/common(q[j],t);//重复计算其公倍数
} printf("%d\n",t);
} return 0;
}

TLE

最大的问题是每次计算都要调用common函数,每次调用都要执行for循环,计算比较多的数导致TLE是正常的事情。

参考代码:by acmcyc

#include <iostream>
using namespace std;
int main()
{
int n,i;
int a[100];
while(cin>>n)
{
int max;
cin>>a[0];
max=a[0];
for(i=1;i<n;i++)
{
cin>>a[i];
if(a[i]>max)
max=a[i];
}
int k=0,j;
for(j=max;;j++)//2 5 7 j=7;
{
for(i=0;i<n;i++)
{
if(j%a[i]!=0)
break;
}
if(i==n)// j%a[i]==0
break;
}
cout<<j<<endl; }
return 0;
}

看到这串代码,不得不佩服作者的思考角度和方法:先找出输入所有数中的最大数max,然后不断+1进行判断(最小公倍数大于等于max),如果找到一个数对输入的所有数都能整除,那么这个数就是他们的最小公倍数。

hdoj-2028-Lowest common multiple plus的更多相关文章

  1. hdu 2028 Lowest Common Multiple Plus(最小公倍数)

    Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. HDU 2028 Lowest Common Multiple Plus

    http://acm.hdu.edu.cn/showproblem.php?pid=2028 Problem Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个 ...

  3. #2028 Lowest Common Multiple Plus

    http://acm.hdu.edu.cn/showproblem.php?pid=2028 应该是比较简单的一道题啊...求输入的数的最小公倍数. 先用百度来的(老师教的已经不知道跑哪去了)辗转相除 ...

  4. 杭电 2028 ( Lowest Common Multiple Plus )

    链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2028 题目要求:就是求最大公倍数,我百度了一下,最好实现的算法就是:       公式法 由于 ...

  5. 最大公约数最小公倍数 (例:HDU2028 Lowest Common Multiple Plus)

    也称欧几里得算法 原理: gcd(a,b)=gcd(b,a mod b) 边界条件为 gcd(a,0)=a; 其中mod 为求余 故辗转相除法可简单的表示为: int gcd(int a, int b ...

  6. Lowest Common Multiple Plus

    Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...

  7. HDOJ 1019 Least Common Multiple(最小公倍数问题)

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  8. 2028 ACM Lowest Common Multiple Plus

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2028 思路:最一想到的就是暴力求解,从1开始一直到最后的答案,一直来除以给出的数列的数,直到余数为0:当然 ...

  9. 【HDU 2028】Lowest Common Multiple Plus

    Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最小公倍数 ...

  10. HDU2028 Lowest Common Multiple Plus

    解题思路:最近很忙,有点乱,感觉对不起自己的中国好队友.   好好调整,一切都不是问题,Just do it ! 代码: #include<cstdio> int gcd(int a, i ...

随机推荐

  1. 在系统启动时,Windows Vista 中、 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TIME_WAIT 状态的所有 TCP/IP 端口

    在系统启动时,Windows Vista 中. 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TI ...

  2. 四、Mosquitto 高级应用之用户配置

    本文将讲解 Mosquitto 用户机制.如果还没有搭建 Mosquitto 服务的可以参考我的另外两篇文章<< 一.Mosquitto 介绍&安装>> << ...

  3. iOS UI基础-6.0 UIActionSheet的使用

    UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件. 使用 1.需要实现UIActionSheetDelegate  协议 @interface NJWisdom ...

  4. Qt QDataTime QString 两个类的使用

    QDateTime now = QDateTime::currentDateTime(); QString nowStr; nowStr = now.toString("yyyyMMdd_h ...

  5. location对象查询字符串参数

    虽然location.search可以返回从问号到URL末尾的所有内容,但却没有办法逐个访问其中的每个查询字符串参数.为此,可以创建下面这样一个函数,用以解析查询字符串,然后返回包含所有参数的一个对象 ...

  6. #C++初学记录(算法2)

    A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...

  7. ExtJs中XTemplate使用(转)

    转自http://www.studyofnet.com/news/408.html 本文导读:XTemplate是Ext.Template扩展的新类,它支持高级功能的模板类,如自动数组输出.条件判断. ...

  8. CMPXCHG指令

    一.CMPXCHG汇编指令详解. 这条指令将al\ax\eax\rax中的值与首操作数比较: 1.如果相等,第2操作数的直装载到首操作数,zf置1.(相当于相减为0,所以0标志位置位) 2.如果不等, ...

  9. FastDFS+nginx+keepalived集群搭建

    安装环境 nginx-1.6.2 libfastcommon-master.zip FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastd ...

  10. SoapUI 使用变量

    登录问题不好解决, 只能临时用cookie来执行 1.变量定义 2.引用变量 3.调用Header