Common basic algorithm C + + implementation

Source: Internet
Author: User

October 06, 2016 10:40:43

This article records some commonly used basic algorithms, only for practice practice , the content of the words will be indexed

Prime (prime number) judgment

The definition of prime number: that is, except for itself and 1, there are no other approximate numbers

1 BOOLIsPrime (inti)2 {3      for(intj =2;J <= sqrt (i * 1.0); J + +) {4         if(i% J = =0)5             return false;6     }7     return true;8}
Greatest common divisor

For example: for the greatest common divisor of 24 and 60, first decomposition of factorization, 24=2x2x2x3,60=2x2x3x5,24 and 60 of all the public quality factor is 2, 2, 3, their product is 2x2x3=12, so, (24,60) =12

1 intgcdintAintb//Greatest Common Divisor2 {3      return(! b) X:GDC (y,x%y);4 }5 6 //or more easily understood.7 intgcdintAintb//Greatest Common Divisor8 {9      while(a%b) {Ten         intTMP =A; OneA =b; Ab = tmp%b; -     } -     returnb; the}

With the greatest common divisor, least common multiple is very good, expressed as a*b/greatest common divisor

Multiply by large number

The read and output of the large number are different from the normal number, and the read-in is a string that is read in, the multiplied number is deposited into the array, and then the output data of the array is traversed.

Let's take a look at the principle of multiplication.

A = B = 25

A and b multiplication process show

B 2 5

A 3 4

----------------------------------------the multiplication process, just like we do in our cognition, there is no rounding.

8 (2x4) (4X5)

6 (2X3) (3X5)

---------------------------------------------------------the normal multiplication, still not carry

6 23 20

---------------------------------------------------------can get the answers we're familiar with by rounding 850

8 5 0

(6+2 (rounding)) (3+2 (rounding)) rounding explanation

Through the above analysis, we know the core idea of the algorithm , then we can implement the algorithm, the implementation method is as follows:

1 voidMultiplyConst CharBConst Char*b) {2ASSERT (A! = NULL && b! = null!);3      intI, J, LA, LB;4La =strlen (a);5LB =strlen (b);6      intS[LA+LB] = {0};7       for(i =0; I < LA; i++) {//complete multiplication, but no rounding8           for(j =0; J < lb; J + +){9S[I+J] + = (a[i)-'0') * (b[j]-'0');Ten          } One      } A       for(i =0; i < la+lb; i++) {//Complete Rounding -s[i+1] + = s[i]/Ten; -S[i] = s[i]%Ten; the      } -       for(i = la+lb; I >=0; i-- ){ -          if(S[i]! =0) cout <<S[i]; -      } +}

Common basic algorithm C + + implementation

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.