Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the maximum common divisor in C language

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to find the maximum common divisor in C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Introduction to C language 1.1 History of C language

C language is a widely used process-oriented computer programming language, which is suitable for both system programming and application programming. The development of C language is roughly shown in figure 1-1:

Figure 1-1 the development of C language

1.2 Features of C language

C language is a general programming language, which is concise, flexible and expressive. It is widely used in the development of system software and application software, and has good portability.

The characteristics of C language can be summarized as follows:

(1) succinct, compact and flexible. The core content of C language is very little, there are only 32 keywords and 9 kinds of control statements; the program is written in a free format, compressing all unnecessary components.

(2) the expression is concise and practical. C language has a set of powerful operators, up to 44, can construct many forms of expressions, with one expression can achieve the functions that other languages may need multiple statements to achieve.

(3) there are abundant data types. The more data types, the stronger the expressive ability of the data. C language has a variety of data types, such as character type, integer type, real type, array, pointer, structure and common body, etc., which can realize various complex data structures such as linked list, stack, queue, tree and so on. The pointer type makes the transfer of parameters simple and fast, while saving memory space.

(4) it has the characteristics of low-level language. C language has similar functions and description methods to assembly language, such as address operation and binary digital operation, etc. It can also directly operate resources such as hardware ports and make full use of computer resources. C language not only has the characteristics of high-level language easy to learn and master, but also has the ability of machine language or assembly language to operate hardware. Therefore, C language can be used as not only a system description language, but also a general programming language.

(5) C language is a structured language, which is suitable for the modular design of large-scale programs. C language provides basic control statements for writing structured programs, such as if-else statements, switch statements, while statements, do-while statements and so on. C language is a collection of functions, and functions are the basic units that constitute C language programs. Each function has an independent function, and data is transmitted through parameters between functions. Programmers can write their own functions. At the same time, compilers of different operating systems provide programmers with a large number of standard library functions, such as input / output functions, mathematical functions and string processing functions. Flexible use of standard library functions can simplify programming and improve programming efficiency.

(6) various versions of compilers provide preprocessing commands and preprocessors. Preprocessing extends the function of C language, improves the portability of the program, and provides convenience for the debugging of large programs.

(7) good portability. Programs can be ported from one environment to a completely different environment without change or slight change. This is because the standard library functions and preprocessors separate the possible machine-related factors from the source programs, so that the relevant contents can be redefined for different computer hardware environments.

(8) the generated target code is of high quality. The running efficiency of the object code compiled and linked by the C source program is only 10% and 20% lower than that written in assembly language, which can give full play to the efficiency of the machine.

(9) the grammar restriction of C language is not strict, and the degree of freedom of programming is large. C language programs do not do checks such as array subscript out of bounds and variable type compatibility at run time, but the programmers themselves ensure the correctness of the program. C language allows almost all data types to be converted, character types and integers can be mixed freely, all types can be logical types, new types can be defined by themselves, and certain types can be forced to be converted to specified types. In fact, this makes the programmer have more autonomy, can write flexible and high-quality programs, and at the same time increases the difficulty for beginners. Therefore, only after being proficient in C language programming, can we realize its flexibility.

C language also has the following disadvantages:

(1) the error of the program is more hidden. The flexibility of the C language makes it more error-prone when writing programs in it, and C's compilers do not check for such errors. Similar to assembly language, these logic errors need to be found when the program is running. The C language also has some hidden dangers, such as writing the comparative "=" as an assignment "=". Although the syntax is correct, such logic errors are often difficult to find, and it is often time-consuming to find them.

(2) C language programs are sometimes difficult to understand. C language is a small language with relatively simple grammatical components. However, it is difficult to understand it because of its many data types, rich operators and various associations.

(3) C language programs are sometimes difficult to modify. Considering the large or large scale of programs, programming languages now usually provide language features such as "classes" and "packages", which can decompose the program into more manageable modules. However, C language lacks such features, so it is difficult to maintain large programs.

1.3 algorithm and its representation

When solving problems in C language, there are two aspects of description in the program, namely, data description and processing steps (algorithm) description, and the latter deals with the data of the former.

The algorithm has the following characteristics:

Finite: the algorithm ends after performing a limited number of steps, and each step can be completed in a finite amount of time.

Certainty: each operation in the algorithm must have an exact meaning, that is, there is no ambiguity. At the same time, in any case, the algorithm has only one execution path, that is, the same input can only get the same output.

Feasibility: the operations described in the algorithm can be realized by performing a limited number of basic operations that have been implemented.

Input: there are zero or more inputs, that is, the necessary information required by the algorithm.

Output: there is one or more outputs that output information that has some specific relationship with the input. Algorithms without output are meaningless.

Representation of the algorithm:

1. Natural language description

two。 Traditional flow chart

3.N-S flow chart

4. Pseudo code.

[for example] find the maximum common divisor of two positive integers m and n (that is, the largest positive integer that can divide m and n at the same time).

1. Natural language description

Euclid explained the process of finding the greatest common divisor of two numbers-- Euclid algorithm.

Step 1: divide m by n and make r the remainder (obviously n > r

0).

The second step: if rsquo, the algorithm ends, n is the largest common divisor of m and n.

Step 3: set mn,nr and return to the first step.

two。 Traditional flow chart

Figure 1-2 A traditional flow chart for finding the greatest common divisor

3. Nmurs flow chart

Figure 1-3 Nmurs flow chart for finding the greatest common divisor

4. Pseudo code

The algorithm begins by inputting m ← n ← do {r TX do {r min divided by m; m ≠ n; n ← r;} while (r ≠ 0); output m; introduction to common algorithms at the end of the algorithm

1. Enumeration method

Enumeration method is also called exhaustion method. This method finds out the real solution of the problem by examining all the possible solutions of the problem one by one. The enumeration method requires that the possible solutions of the problem must be limited, and these possible solutions are known.

Given a positive integer, determine whether its cube root exists, and if so, find out the cube root.

The algorithm starts by inputting a positive integer to n ≤ x ← 0 position while (x ≤ n and x*x*x ≠ n) {x ← x root 1;} if (x ≤ n) finds the integer cube root of n and outputs the value of x; else outputs that the integer cube root of n has no information; the algorithm ends.

two。 Recursive method

The recursive method starts from the known initial conditions and deduces the intermediate results one by one. In the ideal state, each recursion, the result is gradually close to the final solution of the problem. Recursive method is also called iterative method in numerical algorithm. The iterative method is often used to solve the problem of approximate solution. according to the different methods to deal with the error of the previous step, the iterative method has different methods, such as approximate iteration and trial iteration. In numerical calculation, attention should be paid to the stability of the solution, that is, the solution of each step in the iteration is getting closer and closer to the real solution, otherwise the iteration will not be successful.

Calculate the factorial of a positive integer n.

The algorithm begins to input a positive integer to n ≤ t ← 1 position I ← 1 while (I ≤ n) {t ←; I ← 1;} output result t algorithm ends

3. Recursive method

An algorithm that directly or indirectly calls a procedure (or function) itself is called a recursive algorithm, and a function is called a recursive function if it calls itself for calculation. In the algorithm description of some problems, the recursive method is often more concise and easier to understand than the non-recursive method.

Calculate the factorial of a positive integer N.

The recursive definition of factorial function f is as follows: F (1) = 1 (1) if N > 1) f (N) = N (N) = N (N) (N *) (N > 1)

In addition to the enumeration method, recursion method and recursion method introduced above, there are backtracking method, greedy method, divide and conquer method, dynamic programming method and so on.

This is the end of the content of "how to find the greatest common divisor in C language". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report