C language initial learning (4) and C Language Learning
Question: calculate the maximum common divisor of a and B.
Analysis: first, we need to know what the maximum common divisor is, that is, the largest total divisor of two or more integers. Now that you know what the maximum common divisor is, you can solve it. It is equivalent to comparing the two numbers of dikes, and taking the same largest one is the right one, right? What is an appointment ??? The divisor is the number that can divide a or B ~
Okay ~ Let's take a look at the code ~
Code:
1 # include <stdio. h> 2 void gcd (long int a, long int B) // calculate gcd 3 {4 long int I, t = 0, j = 0, k = 0, max, q = 0; 5 long int x [256], y [256]; // x [] stores the divisor of the first number, y [] Stores 6 for (I = a; I> 0; I --) where the second number is divisible by the first Divisor --) // x [] 7 {8 if (a % I = 0) 9 x [j ++] = I; 10 t ++; 11} 12 for (I = 0; I <t; I ++) // y [] 13 {14 if (B % x [I] = 0) 15 y [k ++] = x [I]; 16 q ++; 17} 18 max = y [0]; // select the maximum 19 for (I = 0; I <q; I ++) 20 {21 if (max <y [I]) 22 {23 max = y [I]; 24} 25} 26 printf ("gcd = % ld \ n", max); // output 27} 28 void main () 29 {30 long int a, B; 31 long int x [100], y [100]; 32 while (1) 33 {34 printf ("please enter two number between 0 to 255 \ n"); // enter two numbers 35 scanf ("% ld", &, & B); 36 gcd (a, B); 37} 38}
Let's take a few simple answers:
Is it easy?
Welcome to contact my QQ: 2516985331
Learn Together ~