There are many methods to calculate the maximum common divisor of two numbers, as shown in the following recursion method:
Int Gcd ( Int N, Int M)
{
If (N < M)
{
N = M + N;
M = N - M;
N = N - M;
}
If (M = 0 ) Return N;
Return Gcd (m, n % M );
}
Below I use an array to write a maxtimedest that calculates nCode:
Int Max_common_divisor ( Int Several [], Int N)
{
Int A = Several [ 0 ];
Int B = Several [ 1 ];
Int C = Gcd (A, B );
Int I;
For (I = 2 ; I < N; I ++ )
{
C = Gcd (C, several [I]);
}
Return C;
}
Write a main function to test it:
# Include < Stdio. h >
# Include < Stdlib. h >
Int Gcd ( Int A, Int B ); // Returns the maximum common divisor of two numbers.
Int Max_common_divisor ( Int Several [], Int N ); // Returns the maximum common number of N numbers.
Int Main ()
{
Int A [ 4 ];
Int X, Y;
A [ 0 ] = 147 ;
A [ 1 ] = 210 ;
A [ 2 ] = 315 ;
A [ 3 ] = 84 ;
Printf ( " The maximum number of common appointments is % d \ n. " , Max_common_divisor (, 4 ));
Return 0 ;
}
The test results are as follows: