Calculate the maximum common approx.

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.