In middle school, I did not read mathematics well. Except for the point that the teacher talked about in class, the book was basically not flipped. Now I have a bit of regret. I plan to repeat the middle school mathematics book in a short time.
Greatest common divisor (GCD) is used to obtain the maximum common divisor. The solutions in memory are vague. The division of the moving phase is known only after the C language is learned. It is not clear about the derivation process, only one theorem is known: gcd (a, B) = gcd (B, A mod B ). The Theorem means that the maximum common divisor of A and B is equal to the maximum common divisor of the remainder obtained by Division B of B and. Therefore, finding the remainder is the key.
Well, as I have already said, I am not good at mathematics. If I analyze it again, I will see the code:
//
// Calculate the maximum common divisor by moving phase division. The dependence theorem of the calculation principle is: gcd (a, B) = gcd (B, A mod B)
//
# Include <iostream>
# Include <cstdlib>
Using STD: cout;
Using STD: CIN;
Using STD: Endl;
//
// Maximum common divisor (GCD)
//
Int gcd (int A, int B)
{
Int temp;
If (A <B)
{
Temp =;
A = B;
B = temp;
}
While (0! = B)
{
Temp = A % B;
A = B;
B = temp;
}
Return;
}
Int main (void)
{
Int A = 0, B = 0;
Cout <"Please input two number to computing they GCD :";
While (! (CIN> A> B ))
{
Cin. Clear ();
Cin. Sync ();
Cout <"input error! "<Endl;
Cout <"input againt:" <Endl;
}
Cout <"The GCD is" <gcd (a, B) <Endl;
Return 0;
}