Novice Learning Algorithm--simple exchange and greatest common divisor algorithm introduction

Source: Internet
Author: User
Tags greatest common divisor

After work we spent most of our time studying how to learn a language how to master a technology, but as the fundamental data structure and algorithm of programming we slowly ignored.

After the work of many programmers really do not have the same time for college students to calm down to add their own details, this is my deep understanding of the things of course, I mean here and I have the feeling of exhausted people.

Learning is a process, from simple to complex consistent, recorded only to record their own points drip.

The essence of the algorithm is not the programmer to create the algorithm, but we follow the idea of the ancestors created by the algorithm of code to implement the algorithm.


Here's a quick introduction to two simple examples where one is to exchange one is the greatest common divisor algorithm.

A simple exchange

#include "stdafx.h" #include <stdio.h>/* Our goal is to exchange the ABCD value for the BCDA function M1 M2 Here The code is believed to be a more pediatric code, but the pediatric code is actually a child  The wording of the section. We are writing code to ensure that the two-point efficient and fast minimum loss completion function.  */int a = 1, b = 2, c = 3, d = 4;int Arr[4] = {1, 2, 3, 4};//abcd the value becomes Bcda#define M1 (ta,tb,tc,td) int tem; TEM=TD;             The value of TD=TA;TA=TB;TB=TC;TC=TEM//ABCD becomes Bcda#define M2 (TA,TB,TC,TD) ta+=td;td=ta-td;ta-=td;  TA+=TC;TC=TA-TC;TA-=TC; TA+=TB;TB=TA-TB;TA-=TB; #define RESET (TA,TB,TC,TD) ta = 1; TB = 2; TC = 3;        TD = 4; int _tmain (int argc, _tchar* argv[]) {//First replace the ABCD with the value BCDA the idea of replacing a lot of novices with T=a a=b b=t~//There is a waste of space in the middle of it-the T-middle variable actually we can to A=a+b; B=a-b;     A=a-b; We can write this code in printf ("M2----ABCD value:%d,%d,%d,%d\n", A, B, C, D); M2 (A, B, C, D);p rintf (the value of "M2----ABCD:%d,%d,%d,%d\n", A, B, C, D);p rintf ("\ n"); RESET (A, B, C, d);//different situations we use different ways to solve, for two variables of the exchange we can use the above without intermediate variables to exchange seemingly reduced space consumption, but four variables? The exchange of four variables seems to have changed//implemented such as M1 although I did not achieve the bad but it is really reduced the traditional means of exchange of the code in fact, only when everyone in contact with the program will write ~ ~ ~ haha This also confirms the fact that the complexity of the code implementation of a lot of things we want to do the least amount of code to achieve the minimum number of calculations. printf ("M1----ABCD values:%d,%d,%d,%d\n", A, B, C, D);  M1 (A, B, C, D);p rintf (the value of "M1----ABCD:%d,%d,%d,%d\n", A, B, C, D); return 0;}

The following is an algorithm Euclidean algorithm for seeking greatest common divisor

#include "stdafx.h"////greatest common divisor//algorithm Euclidean algorithm application//Get greatest common divisor//Now the greatest common divisor of AB can be divisible by a and B numbers//If you do not know Euclid's algorithm then it may loop to solve the problem ... So bad//the algorithm nature of our programmers is to translate mathematical algorithm ideas into code that's the essence of most of the algorithms we use  unless you're an algorithmic researcher.//Take a look at this program if you don't know  the greatest common divisor algorithm derived from Euclid's algorithm How do you write the program? Maybe it's multiple loops, ~int   greatestcommondivision (int a, int b) {  //cannot be negative if (a < 0 | | b < 0) return-1;///guarantee a must be greater than equals bif (A < b) {a = a + B;b = A-b;a = A;} int r = 0;//Take remainder if the remainder equals 0 then greatest common divisor is both the current a while ((R = a%b)! = 0) {  ///otherwise a<-b   b<-r    Here the remainder R must be less than B of a = B;b = R; }//is equal to 0 and exits return  B; int _tmain (int argc, CHAR**ARGV) {int a = N, b = 220;printf ("%d and%d greatest common divisor are:%d\n", A, B, greatestcommondivision (A, b)); retur n 0;}

Simple introduction to here, fur technology slowly accumulate one day will have the harvest drops




Novice Learning Algorithm--simple exchange and greatest common divisor algorithm introduction

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.