C-Language dichotomy finds the specified number in a series

Source: Internet
Author: User

In the Pang Maogo teacher's courseware, there are a lot of specific algorithm examples, here, tracking gives, the binary method to find the specified number of code:

    • Implementing the Environment: Ubuntu 17.4 under VMware
    • Compilation environment: Comes with a compiler.

Below is the procedure for compiling the environment under Ubuntu:

    1. Press "ALT" + "CRTL" + "T" at the same time to eject the terminal interface
    2. Create: Command gedit main.c gedit Create a C file with the name main
    3. Edit: Command Gtouch main.c pop-up text box for editing
    4. Compile: "ALT" + "F4" to turn off the text box, enter the terminal interface, command GCCMAIN.C to compile, if there is no error, the next command prompt g++ is the C + + file compilation
    5. Result:./a.out to view

Here is an example of a simple dichotomy lookup:

1#include <stdio.h>2 Main ()3 {4  intnum[]={1,2,3,4,5,6,7,8,9,Ten};5  intleft=0, right=9, A;6  intfind;7scanf"%d",&find);8   while(left<Right )9  {TenA= (left+right)/2; One   if(num[a]==find) A   { -printf"The number you are looking for is in the first%d", A +1); -     Break; the   } -   Else if(find>Num[a]) -left=a+1; -   Else +right=a-1; -  } +  if(left<Right ) Aprintf"without this number");  at}

C-Language dichotomy finds the specified number in a series

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.