#include <iostream>using namespacestd;//Euclidean algorithm for greatest common divisor of two nonnegative integersintGetdivisor (intAintb) { intmax,min; Max=A; Min=b; //two number of large number modulus decimal, if the result is not 0, then discard the large number, the decimal and the result of the modulo operation to size, continue to take the modulo operation//recursive solution in turn until the modulo operation result is 0, then the decimal is greatest common divisor if(max%min!=0){ if(max%min>min)returnGetdivisor (max%min,min); Else returnGetdivisor (min,max%min); }Else returnmin;}intMain () {//freopen ("D:\\algorithm\\testdata.txt", "R", stdin); inta,b,max,min; intresult; cout<<"Please enter two non-negative integers:" ; CIN>> a >>b; A>b? (max = A,min = b):(max = B,min =a); Result=Getdivisor (A, b); cout<< a <<"with the"<< b <<"the greatest common divisor for"<< result<<Endl; return 0; }
Two nonnegative integers for greatest common divisor (Euclidean algorithm)---C + +