Data structure and algorithm analysis C + + Presentation Chapter II Programming Questions

Source: Internet
Author: User

Consolidate the second chapter of yesterday's reading and do a programming exercise.

2. 6:

The first day to pay 2 yuan fine, after each day is the square, the day before the second, the number of fines will be?

This topic is basically the same as the power operation introduced by 2.4.4-3. If the same recursive thinking analysis, than the problem is simple, because from 1 power and exponential 2^ (n-1) distribution, namely 1,2,3,4,16 ... So there is no judgment when the exponent is odd. In fact, it is quicker to use a loop to find than to use recursion. Without considering the overflow, the solution is as follows:

#include <iostream>using namespacestd;typedef unsignedLong LongUint64;uint64 R (intN) {    if(n==1){        return 2; }Else{UInt64 tmp=r (n1); returntmp*tmp; }}uint64 W (intN) {    intresult=2;  while(n!=1) {result*=result; N--; }    returnresult;}intMain () {intN; CIN>>N; cout<<r (n) <<" "<<w (n) <<Endl;}

2. 20:

Determines whether a positive integer is a prime number. This problem can be simplified because n*n> (n-1) * (n+1). So just ask if n can be divisible by 2 to sqrt (n), and it's important to note that 2 is the smallest prime number:
The For (I=2;I<SQRT (n); i++) cannot return the correct result for 2 because 1.414<2 does not enter the loop. So to judge first:

#include <iostream>#include<cmath>using namespacestd;BOOLIspnintN) {    if((n%2)==0){        return false; }Else{        inti,max=int(sqrt (n));  for(i=3; i<=max;i++){            if((n%i) = =0)return false; }    }    return true;}intMain () {intN; CIN>>N; cout&LT;&LT;ISPN (n) <<Endl;}

Of course, you can also if (n==2) and then for (i=2;i<=max;i++). It is important to note that the square root (max) condition is not less than, and is less than or equal to. Then there is the time complexity of the Eratosthenes sieve, where only the Eratosthenes sieve is recorded: The total number of elements from 2 to n,n>=2 only needs to be filtered out of those non-prime numbers. According to the principle described earlier, the method of removing these non-primes is as follows: The loop removes all non-primes between 2-SQRT (n):

#include <iostream>#include<cmath>#include<vector>using namespacestd;typedef unsignedLong LongUInt64;voidES (vector<int> &arr) {    intI,j,n=arr.size (), sqrtnum=int(sqrt (n));  for(i=2; i<=sqrtnum;i++) {//traversing to sqrt (Arr.size ()) starting from the smallest prime number m (m=2)        if(arr[i]==0){//find the back first prime number m (Mark still 0)             for(j=i*2; j<=n;j+=i) {//the tag m*n (1<n<=arr.size/m) is a non-prime number. arr[j]=1; }        }    }}intMain () {intI,n; CIN>>N; Vector<int> Arr (n+1);    ES (arr);  for(i=2; i<=n;i++){        if(arr[i]==0) {cout<<i<<Endl; }    }}

Note the following points (set N to the upper limit):

1, the code in the default value of arr is 0, in order to represent the assumed prime number, the final result is marked 1 is not a prime, its subscript corresponds to the number one by one, even if input 11, the array is the largest subscript 11, that is, the array element is n+1.

2, according to the previous conclusion, I only need to search from 2 to sqrt (n), including sqrt (n).

3, when the flag, from the current prime number of twice times the beginning of the sign.

In Chinaunix above see a similar algorithm, 1, 2 have committed, only inefficient output n-1 within, missing N. For example, enter 11, get 2, 3, 5, 7, such as input 2 nothing came out.

Take a break, January 1, 2017 12:45, above.

Data structure and algorithm analysis C + + Presentation Chapter II Programming Questions

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.