Search for the number in the array and find the Array

Source: Internet
Author: User

Search for the number in the array and find the Array

There is an int-type array, and the difference between each two adjacent numbers is not 1 or-1. Given a number, we need to find the position of this number in the array.
In fact, the idea is to skip the search, because you know a number, then the second number is 1 at most, and the third number is 2 at most, however, you can use the number of targets minus this number to exclude unnecessary searches. For example, if the number you want is 10 and the first number is 2, the first seven numbers cannot reach 10 in any case, it is only possible to count at 8th, so we can directly judge the number at 8th.
The following is an example of how other people's code borrowed:

Void PrintfArray (int a [], int n) {for (int I = 0; I <n; I ++) printf ("% d", a [I]); putchar ('\ n');} int FindNumberInArray (int arr [], int n, int find_number) {int next_arrive_index = abs (find_number-arr [0]); while (rows <n) {if (arr [next_arrive_index] = find_number) return next_arrive_index; next_arrive_index + = abs (find_number-arr [next_arrive_index]);} return-1 ;}

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.