There is an int array, and the difference between each of the two adjacent numbers is either 1 or-1. Now given a number, it is required to find the position of the number in the array.
In fact, the idea is to jump to find, because you know a number, then it is the second number of the maximum difference of 1, the third number is a maximum of 2, and you can use the target number minus this number to exclude some redundant lookup, such as you want the number is 10, the first number is 2, then its first 7 number is not up to 10 Only the 8th number is possible, so you can directly judge the 8th number.
Here are some other people's code to borrow the idea is probably this:
voidPrintfarray (intA[],intN) { for(inti =0; I < n; i++)printf("%d", A[i]);Putchar(' \ n '); }intFindnumberinarray (intArr[],intNintFind_number) {intNext_arrive_index =ABS(find_number-arr[0]); while(Next_arrive_index < N) {if(Arr[next_arrive_index] = = Find_number)returnNext_arrive_index; Next_arrive_index + =ABS(Find_number-arr[next_arrive_index]); }return-1; }
Find the number inside the array