C language initial learning (4) and C Language Learning

Source: Internet
Author: User

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 ~

Related Article

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.