project euler 5

来源:互联网 发布:淘宝上情趣用品买家秀 编辑:程序博客网 时间:2024/05/21 06:24

题目:

https://projecteuler.net/problem=5

题意:

Smallest multiple
Problem 5
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

求1~20这20个数字的最小公倍数

思路:

初始化ans=1,然后求ans=lcm(ans,i)(1<=i<=20)即可

代码:

#include <bits/stdc++.h>using namespace std;int gcd(int a, int b){    return !b ? a : gcd(b, a%b);}int lcm(int a, int b){    return a / gcd(a, b) * b;}int main(){    int ans = 1;    for(int i = 1; i <= 20; ++i)        ans = lcm(ans, i);    printf("%d\n", ans);    return 0;}
原创粉丝点击