解法一:循环倒叙一个个找
#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 >= 1; i--) { //i能被n和m整除,说明i同时为n,m的因子 if(n % i == 0 && m % i == 0) { cout << i; return 0; } } return 0; }
解法二:辗转相除法gcd
#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 << gcd(n,m); return 0; }
标签:找出,gcd,int,ll,最大公约数,3169,cout From: https://www.cnblogs.com/jyssh/p/18277124