Codeup2136 HDU1019 Least Common Multiple

#include<iostream>
using namespace std;

int gcd(int a, int b)
{
	if (b == 0)
		return a;
	else
		return gcd(b, a%b);
}

int lcm(int a, int b)
{
	return a / gcd(a, b)*b;//先除再乘,减少溢出
}

int main()
{
	int n;
	int m;
	int number1, number2;
	cin >> n;
	while (n--)
	{
		cin >> m;
		cin >> number1;//写的好处,省去用数组
		for (int i = 1; i < m; i++)//第一数,先输入了所以i从1开始
		{
			cin >> number2;
			number1 = lcm(number1, number2);//隐藏的递归,妙!
		}
		cout << number1 << endl;
	}

	return 0;
}
时间: 2024-08-24 18:28:58

Codeup2136 HDU1019 Least Common Multiple的相关文章

HDU1019 Least Common Multiple

PS: 如果开始求解 前两个数字的最小公倍数,然后迭代求解下面的则TLE,如果开始求解第一个数字和1的LCM则 0ms. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int gcd(int a, int b) { if(b==0) return a; else return gcd(b, a%b); } i

HDU1019 Least Common Multiple(多个数的最小公倍数)

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. InputInput will consist of multiple problem instances. The fi

ACM-简单题之Least Common Multiple——hdu1019

***************************************转载请注明出处:http://blog.csdn.net/lttree*************************************** Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28975    

hdu 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54584    Accepted Submission(s): 20824 Problem Description The least common multiple (LCM) of a set of positive integers is

题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)

题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1439 Least Common Multiple.cpp // Jobdu // // Created by PengFei_Zheng on 10/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. /

ACM学习历程—HDU 3092 Least common multiple(数论 &amp;&amp; 动态规划 &amp;&amp; 大数)

hihoCoder挑战赛12 Description Partychen like to do mathematical problems. One day, when he was doing on a least common multiple(LCM) problem, he suddenly thought of a very interesting question: if given a number of S, and we divided S into some numbers

杭电 HDU ACM 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): 39183    Accepted Submission(s): 16144 Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Ou

HDUJ 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29480    Accepted Submission(s): 11136 Problem Description The least common multiple (LCM) of a set of positive integers is

Lowest Common Multiple Plus(杭电2028)

/*Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33241    Accepted Submission(s): 13578 Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.