HDU1019 Least Common Multiple (最小公倍数)

HDU1019 Least Common Multiple (最小公倍数)

原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1019

题意

给定n个数,求这些数的最小公倍数

思路

先将给定的数按从小到大进行排序,然后设置一个索引index,表示求前第几个数的最小公倍数,设置两个变量now和multiple,now存前index个数的公倍数,multiple存前index个数的最小公倍数。

如果now能够被第index个数整除,则代表now是前index个数的最小公倍数,将改值赋给multiple;如果不能被整除,则将now+multiple直到可以被整除为止。

因为multiple已经可以被前index个数整除,所以now+multiple必定可以被前index个数整除。

AC代码

#include <bits/stdc++.h>

using namespace std;
/**
 *  Created with IntelliJ Clion.
 *  @author  wanyu
 *  @Date: 2018-02-27
 *  @Time: 15:53
 *  To change this template use File | Settings | File Templates.
 * 
 */
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        int nums[n];
        for (int i = 0; i < n; i++) {
            scanf("%d", &nums[i]);
        }
        sort(nums, nums + n);
        int multiple = nums[0];//前index个数的最小公倍数
        int now = nums[0];//前index个数的公倍数
        int index = 1;//索引
        while (index < n) {
            if (now % nums[index] == 0) {
                //是倍数
                multiple = now;
                index++;
            } else {
                //不是倍数
                now += multiple;
            }
        }
        printf("%d\n", multiple);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值