zoukankan      html  css  js  c++  java
  • 二分查找

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 
     5 int main()
     6 {
     7     int a[10000],n,m,i,j,k,low,high,mid,flag;
     8     scanf("%d",&n);
     9     for(i=0; i<n; i++)
    10         scanf("%d",&a[i]);
    11     scanf("%d",&m);
    12     j=0;
    13     while(j<m)
    14     {
    15         low=0,high=n-1,flag=0;
    16         scanf("%d",&k);
    17         while(low<high&&flag==0)
    18         {
    19             if(high-low==1)
    20             {
    21                 if(k==a[low])
    22                 {
    23                     flag=1;
    24                 }
    25                 else if(k==a[high])
    26                 {
    27                     flag=1;
    28                 }
    29                 else
    30                 {
    31                     flag=2;
    32                 }
    33             }
    34             else
    35             {
    36                 mid=(high+low)/2;
    37                 if(k==a[mid])
    38                 {
    39                     flag=1;
    40                 }
    41                 else if(k<a[mid])
    42                 {
    43                     high=mid;
    44                 }
    45                 else if(k>a[mid])
    46                 {
    47                     low=mid;
    48                 }
    49             }
    50         }
    51         if(flag==1)
    52             printf("Yes
    ");
    53         if(flag==2)
    54             printf("No
    ");
    55         j++;
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    由一段代码说开去——set
    由一段代码说开去——list
    day 11
    day 16
    day 10
    day 13
    day 9
    day 18
    day 12
    day 14
  • 原文地址:https://www.cnblogs.com/zhouyee/p/4396081.html
Copyright © 2011-2022 走看看