解法一:循环找
#include<bits/stdc++.h> #define f(i,s,e) for(int i = s; i <= e; i++) #define ll long long using namespace std; const int N = 1e3+10,inf = 0x3f3f3f3f; int main() { int n,m; cin >> n >> m; for(int i = n; i <= n * m; i++) //在(n,n*m)范围找公倍数 { //i同时是n,m的倍数 if(i % n == 0 && i % m == 0) { cout << i; return 0; } } return 0; }
解法二:辗转相除法, 公倍数 = n * m / gcd(n,m)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e3+10,inf = 0x3f3f3f3f; int gcd(int a,int b) //gcd(a,b) 求出a和b的最大公约数 { return b == 0 ? a : gcd(b,a % b); } int main() { ll n,x,y,m; cin >> n >> m; cout << n * m /gcd(n,m); return 0; }
标签:找出,gcd,公倍数,ll,long,int,3170 From: https://www.cnblogs.com/jyssh/p/18277119