hdoj 1019 最小公倍数问题

我最初以为这是大树乘除法问题,后来发现只是普通的求最小公倍数方法

原题:

Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 38411    Accepted Submission(s): 14484

Problem 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

wa码

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#define max 100
using namespace std;
int cmp(int a,int b)
 {
 	return b<a;
 }

long long a[max],s;
int m,n,i,j;
int main()
{
	scanf("%d",&n);
	while(n--)
	{    s=1;
	<span style="white-space:pre">	</span>scanf("%d",&m);
		for(i=0;i<m;i++)
		scanf("%d",&a[i]);
		sort(a,a+m,cmp);	

		for(i=0;i<m;i++)
		{
			for(j=i+1;j<m;j++)
			{
				if((a[i]%a[j])==0)

				a[j]=1;

			}
		}
		for(i=0;i<m;i++)
		s=s*a[i];
		printf("%d\n",s);
	}
	return 0;
}

总是出错,于是改啊改,发现算法就是错的,我的只能算15 5 7,正好谁是谁的公倍数的,却算不来哦36 8 14这种(此时虽然8 14不能被36 整除,但是他们存在最大公约数),要用我的算法,应该等于36*14*8,显然不对!

我的AC码

<span style="font-size:24px;">#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#define max 100
int main()
{
	int a,n,m,i,s;
	scanf("%d",&n);
	while(n--)
	{   s=0;
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d",&a);
			if(s==0)//</span><span style="font-size:14px;">先初始化让s西安等于第一个数的值</span><span style="font-size:24px;">
			 s=a;
			for(i=s; ; i=i+s)//</span><span style="font-size:14px;">特别巧妙的算法,用i每次都乘以整数倍的本身,当i被a整除时恰好i是两者的最大公倍数
			{</span><span style="font-size:24px;">
				if(i%a==0)
				break;
			}

		}
		printf("%d",s);
	}

		return 0;
}
</span>

意外收获:

误以为qort(a,n,sizeof(a0),cmp)

int cmp (const void *a,const  void *b)

可以用在c和c++都行!

原来这个是c语言的

c++应该是

qsort(a,a+n,cmp)

int cmp (int a,int b)

return a<b 或者a>b

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-07 10:04:48

hdoj 1019 最小公倍数问题的相关文章

hdu 1019 最小公倍数

简单题 注意__int64 的使用 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 Problem : 1019 ( Least Common Multiple )     Judge Status : Accepted RunId : 10599776    Language : C++   

hud 1019最小公倍数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 思路:头两个数先求,再用所求的数与后面的一个数求,依次类推 1 #include<stdlib.h> 2 #include<time.h> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <cstdlib> 7 #include <c

HDOJ 1019

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

数论 HDOJ 5407 CRB and Candies

题目传送门 题意:求LCM (C(N,0),C(N,1),...,C(N,N)),LCM是最小公倍数的意思,C函数是组合数. 分析:先上出题人的解题报告 好吧,数论一点都不懂,只明白f (n + 1)意思是前n+1个数的最小公倍数,求法解释参考HDOJ 1019,2028 这个结论暂时不知道怎么推出来的,那么就是剩下1/(n+1) 逆元的求法了 代码: /************************************************ * Author :Running_Time

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): 30285    Accepted Submission(s): 11455 Problem Description The least common multiple (LCM) of a set of positive integers is

HDU 1019 (多个数的最小公倍数)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 61592    Accepted Submission(s): 23486 Problem Description The least comm

HDU 1019 Least Common Multiple-数论(最小公倍数)

题意:求n个数的最小公倍数 分析:用方法:lcm(a,b,c)=lcm(a,lcm(b,c)).注意先除后乘防止整数溢出(小技巧) 代码: #include<iostream> using namespace std; int t,n,a; int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int main() { cin>>t; while(t--){ cin>>n; cin>>a; int tmp=a;

杭电1019 Least Common Multiple【求最小公倍数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了,于是改成了long long,还是WA,于是就不明白了,于是就去看了discuss,发现应该这样来写 lcm(a,b)=a*gcd(a,b)*b;是为了以防a乘以b太大溢出,注意啊!!!!所以就先除再乘. #include<stdio.h> int gcd(int a,int b) { int t

HDU 1019 Least Common Multiple (最小公倍数_水题)

Problem 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 prob