Two classic algorithms-2017 autumn strokes written

Source: Internet
Author: User

Read Catalogue

    • Seeking primes
    • Maximum difference value
Go back to the top to find primes

Enter M, n,1 < m < N < 1000000 to find the number of all primes in the interval [m,n]. Prime number definition: Except 1, only the natural numbers divisible by 1 and themselves are called prime numbers.

Input Description:
Two integers m,n
Output Description:
Number of primes in interval
Example 1 input
2 10
Output
4

#include <iostream>#defineK 1000001using namespacestd;Charp[k+1] = {1,1,0};//Array top three number 0 1 2 composite, composite, prime numbers respectively intMain () {inti,j;  for(i =2; I <= k/Ten; ++i)//prevent p[i*j] from crossing the border     {         if(!P[i]) for(j =2; I*j <=k; ++J)//determine if it is compositeP[I*J] =1;//It's composite .      }           intM,n,count; CIN>>M; CIN>>N; Count=0;  for(i=m; i<=n; i++)         if(!p[i])//if P[i] is composite, then skip, if it is a prime number, execute Countcount++; cout<<count;}

Analysis:

The concept of a prime number in an integer greater than 1 can only be divisible by 1 and by itself.

In integers greater than 1, as long as the number of similar m*n is not prime. Use 1 for non-prime, and 0 for Prime. Then: p[i*j] = 1 is the identification of all non-prime numbers.

K/10 is to prevent p[i*j] cross-border, of course, divided by 20, 30 is also possible!

Reference Links:

"Template applet" for prime numbers less than or equal to n range

New Ket NET Solution

Go back to the top of the maximum difference

Given an unordered sequence, find the maximum difference between the two adjacent values in the sorted state of this series, and return 0 with fewer than two values. For example, the maximum difference for a given sequence [1,3,2,0,1,6,8] is 3. Note: Try to use a scheme with a time complexity of O (n).

Input Description:
The first line enters a single integer n as the size of the sequence, and the second line enters all the elements in the series M, a total of n. 0 < N <= 1000000, 0 < M < 2100000000
Output Description:
The maximum difference of the sequence number
Example 1 input
3 1 10 5
Output
5

#include <iostream>#include<vector>#include<algorithm>using namespacestd;intMain () {intN;  while(cin>>N) {Vector<int>Array (N);  for(intI=0;i< (int) array.size (); + +i) {cin>>Array[i];  } sort (Array.begin (), Array.end ()); //Sort Firstvector<int> Chazhi (N);//open an array and deposit the difference between adjacent elementschazhi[0] =0;//Array Initialization     intMax_chazhi =0;  for(intI=1;i< (int) chazhi.size (); + +i) {Chazhi[i]=array[i]-array[i-1]; Max_chazhi= Chazhi[i]>max_chazhi?Chazhi[i]: Max_chazhi; } cout<<max_chazhi<<Endl; }          return 0; }

Analysis:

Study the other people's code, the overall idea is to start the input sequence of small to large sort, and then create an array, the difference between the two adjacent numbers after sorting, then the size of each other, and finally output the maximum difference.

Two classic algorithms-2017 autumn strokes written

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.