A brief discussion on the code of the algorithm design of the postgraduate examination from the problem of circular left shift

Source: Internet
Author: User

Problem: Set N (n>1) integers to be stored in a one-dimensional array R. An algorithm is designed to move the sequence in R to the left P (0<p<n) position, and the data in R is transformed from {x0,x1,...,xn-1} to {xp,xp+1,..., xn-1,x0,x1,..., Xp-1}. Requirements: Write the algorithm description of the subject.

Analysis:

The subject is not difficult, to achieve r in the sequence of the left shift p position, only the first P elements in the R in the inverse, and then the remaining elements of the inverse, the final overall reverse operation can be. The algorithm is described as follows:

1#include <iostream>2 using namespacestd;3 #defineMAX 1004 voidNizhi (intR[],intMintN//to reverse an element from M to n5 {6     inttemp;7      for(intI=m,intJ=n;i<j;++i,--j) {8temp=R[i];9r[i]=R[j];Tenr[j]=temp; One     } A } - voidDiaoyong (intR[],intNintP) {//functions that invoke an inverse operation -     if(p<=0|| P>=n)//determine if the number of left shifts is reasonable thecout<<"ERROR"<<Endl; -     Else -     { -Nizhi (R,0, P-1);//the inverse of an element from r[0] to R[p-1] +Nizhi (r,p,n-1);//the inverse of an element from r[p] to r[n-1] -Nizhi (R,0, N-1);//the inverse of an element from r[0] to r[n-1] +     } A } at intMainvoid) - { -     intn,p; -     intR[max]; -cin>>p;//enter the number of digits left shifted -cin>>n;//Enter the number of total elements in      for(intI=0; i<n;i++)//initializing an array -Cin>>R[i]; toDiaoyong (R,N,P);//calling Functions +      for(intI=0; i<n;i++) -cout<<r[i]<<" ";//output of an array R with a left shift p position thecout<<Endl; *     return 0; $}

The above program is a complete program can be run under the compiler, if the test taker, the test paper on the description of the algorithm, there is no need to write, for the postgraduate selection test, only need to write out the function of the description, give the interface of the function can be, The quiz teacher will know that you have done the tool (function) to solve the problem, and explained how to use the tool (interface). So, we just need to give the code from line 4th to 22, which is obviously a lot more concise.

void Nizhi (int r[],int m,int N)//reverse operation of elements from M to n {int temp;for (int i=m,int j=n;i<j;++i,--j) {Temp=r[i]; R[I]=R[J]; R[j]=temp;}} void Diaoyong (int r[],int n,int p) {//function to invoke an inverse operation if (p<=0| | P>=N)//To determine whether the number of left shift is reasonable cout<< "ERROR" <<endl;else{nizhi (r,0,p-1),//from r[0] to r[p-1 the element between the reverse operation Nizhi (R,p, n-1) (///from r[p] to r[n-1] The inverse operation of the element between the Nizhi (r,0,n-1);//from r[0] to r[n-1].

Through the above description, the interface can be understood as the user and the function of the place, through the interface, the user entered their own data, get the desired results

At this point, we can know that in the comprehensive application of postgraduate examination in the code of the design problem of the key needs to write the content, that is, just write one or more can solve this problem has a clear interface description of the function can be.

A brief discussion on the code of the algorithm design of the postgraduate examination from the problem of circular left shift

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.