[Programming question] An array is formed by moving several digits to the left of a descending sequence. In this array, you can find a number. ☆

Source: Internet
Author: User

48. Microsoft (operation ):
An array is formed by moving several digits to the left of a descending sequence, such}
It is formed by moving two places to the left of {6, 5,}, and you can find a number in this array.

 

My ideas:

Very troublesome: first, use the binary method to locate the largest number, then locate the descending range of the number to be searched, and finally use the common binary search method. The Code is as follows:

/* 48. microsoft (operation): An array is formed by moving several digits to the left of a descending sequence. For example, {,} is formed by moving two places to the left, search for a certain number in this array. */# Include <stdio. h> int find (int * a, int Len, int num) {int begin = 0; int end = len-1; int mid; int maxnumlocation =-1; bool isfind = false; // if (a [0] = num) return 0; else if (a [Len-1] = num) return len-1; else {// first use binary search to locate the maximum position while (end> begin + 1) {if (a [begin]> A [end]) {maxnumlocation = begin; break;} else {mid = begin + (end-begin)/2; if (a [Mid]> A [end]) {end = mid;} Else {begin = mid ;}} if (maxnumlocation =-1) {maxnumlocation = end ;} // locate the decreasing range of the number to be searched if (Num> A [Len-1] & num <A [0]) // The interval has not been rotated. {begin = 0; end = len-1;} else if (Num> A [Len-1] & num> A [0]) // In the right half of the rotation {begin = maxnumlocation; end = len-1;} else if (Num <A [Len-1] & num <A [0]) // {begin = 0; end = maxnumlocation-1;} else {printf ("error !!!! ");} // Binary search while (begin <End-1) {mid = begin + (end-begin)/2; if (a [Mid]> num) {begin = mid;} else if (a [Mid] <num) {end = mid;} else {return mid ;}} if (a [begin] = num) return begin; else if (a [end] = num) return end; else {printf ("error! "); Return-1 ;}}int main () {int A [6] = {6, 4, 2, 0, 10, 8}; int B = find (A, 6, 8); Return 0 ;}

 

I read the answer online and found myself thinking too complicated.

The introduction of http://www.cnblogs.com/daniagger/archive/2012/06/11/2545533.html is quite good

A: If you want to separate the array from the center and divide it into two arraysAt least one array is monotonically decreasing.The other array can be obtained by shifting several digits to the left of the descending array. Therefore, when determining the boundary after the decimal point, we must consider all the conditions, that is, the partition where the array to be searched is located.

First, we need to determine which partition is monotonically decreasing, which can be obtained by comparing arr [l] and ARR [Mid]. If it is greater than or equal, the left partition is monotonically decreasing; otherwise, it is the right partition. Then, you can determine which partition to select by judging whether the value to be searched is in the middle of the descending partition.

Code verification is feasible.

Note: I have thought about classification, But I think too much about it. I always want to find the descending sequence of numbers. In fact, it is not necessary to define a descending sequence boundary,If you can exclude half of the errors at a time, you can perform binary search.!

#include <iostream>using namespace std;int FindData(int* arr,int value,int l,int r){    while(l<=r)    {        int mid=(l+r)/2;        if(arr[mid]==value)            return mid;        else        {            if(arr[l]>=arr[mid])            {                if(value>arr[mid] && value<=arr[l])                    r=mid-1;                else                    l=mid+1;            }            else            {                if(value<arr[mid] && value>=arr[r])                    l=mid+1;                else                    r=mid-1;            }        }    }    return -1;}int main(){    int arr[]={6,4,2,0,10,8};    int n;    cin >>n;    cout <<FindData(arr,n,0,5)<<endl;    return 0;}

 

[Programming question] An array is formed by moving several digits to the left of a descending sequence. In this array, you can find a number. ☆

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.