C + + The smallest number after an ordered array rotation

Source: Internet
Author: User

//The problem is: There are 10,000 employees in the company to work, the company in order to count the age distribution of workers,//Please sort out the age of these 10,000 people and analyze a better algorithm.#include <iostream>#include <vector>#include <stdlib.h>#include <string.h>#include <stdio.h>#define _MAX_AGE_#define _MIN_AGE_ 0#define _EXIT_ cout<< "There is no old age! "; exit ( -1)using namespace STD;voidGrial ( vector<int>AR) {int Array[_max_age_];//Declare an array with a space size of _max_age_.     memset(Array,' + ',sizeof(Array));intn = ar.size (); for(intI=0; i<n;i++) {if(Ar[i]>=_max_age_ | | ar[i]<=_min_age_) {_exit_;}Else{Array[ar[i]]++; }               }//array[n] Array subscript means the age of 1-100, and the number of people at that age is stored under each age. }intMain () { vector<int>Arr for(intI=0;i< -; i++) {Arr.push_back (rand ()% -); } grial (arr);return 0;}#include <iostream>using namespace STD;//Find the smallest number after the ordered array is rotated. intGrial (intA[],intN) {inti =0;intj = N1; while((i+1) {!=j) {intMid = (i+j)/2;if(a[0]&LT;A[J])returna[0];//1 2 3 4 5 6 7 Consideration of the situation.            if(A[i]==a[j] && a[mid]==a[i])//1 1 1 1 1 0 0 1 Consideration of the situation.{ for(; i<=j;i++) {if(A[i]>a[j])returnA[J]; }if(I&GT;J)returnA[i]; }if(a[0]>a[mid])//Then it is compared with the first element, there are 2 cases.{j=mid; }if(a[0]<=a[mid]) {i = mid; }    }returna[i]>a[j]?a[j]:a[i]; }intMain () {inta[]={1,2,3,4,5};cout<<grial (A,5) <<endl;return 0; }

C + + the smallest number after an ordered array rotation

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.