更相减损法和辗转相除法
#include<iostream> using namespace std; //辗转相除 int gcd1(int num1, int num2){ if(num2 == 0) return num1; return gcd1(num2, num1 % num2); } //更相减损 int gcd2(int num1, int num2){ while(num1 != num2){ if(num1 > num2) num1 -= num2; else num2 -= num1; } return num1; } int main(){ int num1, num2; while(cin >> num1 >> num2){ if(num1 < num2) swap(num1, num2); cout << gcd2(num1, num2) << endl; } return 0; }