Greatest common divisor (Harbin Institute of Technology retesting)

Foreword:

21. Regardless of whether you can enter the retest or not, record the garbage code written on the road. I originally gnawed on "Algorithm Notes", but I felt too much to do it, so I changed it to the Kingway Computer Test Guide.

Title description:

Enter two positive integers and find the greatest common divisor.

Enter description

There are multiple groups of test data. Enter two positive integers for each group.

Output description:

For each group of inputs, please output its greatest common divisor.

answer

#include<iostream>
using namespace std;

int main() {
    
    
	int a, b;
	while (cin >> a >> b) {
    
    
		int res = 0;
		while (a != 0 && b != 0) {
    
    
			if (a > b)
				a = a % b;
			else
				b = b % a;
		}
		if (a == 0 && b != 0)
			res = b;
		else
			res = a;
		cout << res << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44897291/article/details/113115966