ZJU Least Common Multiple
来源:互联网 发布:广州数据库培训机构 编辑:程序博客网 时间:2024/06/05 17:48
#include<stdlib.h>#include<stdio.h>int *data;int main(){void lcm(int n);int m;int n;int i;scanf("%d",&m);while(m--){scanf("%d",&n);data=(int *)malloc(sizeof(int)*(n+1));for(i=0;i<n;i++)scanf("%d",&data[i]);lcm(n);free(data);}}void lcm(int n) //求n个数的最小公倍数{int gcd ( int a,int b );int i,j;int tem;int now=data[0];for(i=1;i<n;i++){ tem=gcd(now,data[i]);now=now/tem*data[i];}printf("%d\n",now);} int gcd ( int a,int b ) //求两个数的最大公约数 {int temp;if (a<b){temp=a;a=b;b=temp;}if ( a % b == 0)return b;elsereturn gcd ( b,a % b) ;}
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1797
求多个数的最小公倍数。现在我们对于两个数,他们的最小公倍数和最大公约数有如下关系:
设两个数是a,b最大公约数是p,最小公倍数是q
那么有这样的关系:ab=pq
而最大公约数可以用辗转相除发求。
解题思路是这样的:先求前两个数的最小公倍数q,再求q与第三个数的最小公倍数,覆盖掉原来的q值,,,,,依次下去.................
#include<stdlib.h>#include<stdio.h>int *data;int main(){void lcm(int n);int m;int n;int i;scanf("%d",&m);while(m--){scanf("%d",&n);data=(int *)malloc(sizeof(int)*(n+1));for(i=0;i<n;i++)scanf("%d",&data[i]);lcm(n);free(data);}}void lcm(int n) //求n个数的最小公倍数{int gcd ( int a,int b );int i,j;int tem;int now=data[0];for(i=1;i<n;i++){ tem=gcd(now,data[i]);now=now/tem*data[i];}printf("%d\n",now);} int gcd ( int a,int b ) //求两个数的最大公约数 {int temp;if (a<b){temp=a;a=b;b=temp;}if ( a % b == 0)return b;elsereturn gcd ( b,a % b) ;}
0 0
- ZJU Least Common Multiple
- HDU1019 Least Common Multiple
- Least Common Multiple
- 1019:Least Common Multiple
- 1019 Least Common Multiple
- HDU1019 ( Least Common Multiple )
- Least common multiple(H1019)
- 1019 Least Common Multiple
- Least Common Multiple 1019
- toj1528 Least Common Multiple
- Least Common Multiple
- Least Common Multiple
- hdu Least Common Multiple
- hdoj1019--Least Common Multiple
- AOJ41 Least Common Multiple
- HDOJ1019 Least Common Multiple
- hdu1019 Least Common Multiple
- TOJ:Least Common Multiple
- CSAPP
- HDU 3033 I love sneakers! (01背包+反分组背包)
- 黑马程序员 两小程序引发的思想
- JavaScript source map 的意义
- js:对象的创建(基于组合和动态原型)
- ZJU Least Common Multiple
- NEFU 655 图dp
- 积木城堡_DP
- 使用NotePad++编辑python代码
- vijos1626 爱在心中——by rfy
- UVA - 11525 Permutation
- TOP 10 开源的推荐系统简介
- android viewpager
- Problem 10:Summation of primes