aboutsummaryrefslogtreecommitdiff
path: root/tests/sysy_lvX/1086_bin_search.sy
blob: ce8647564d77e042e9cf0fea861212c288128587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

int main()
{
    //newline=10;
    int i;
    int sum;
    int a[10];
    sum=0;
    //m = 1478;
    //int t;
    i=0;
    while(i<10)
    {
        a[i]=i+1;
        i=i+1;
    }
    int x;
    int high;
    int low;
    int mid;
    int n;
    n=10;
    x=getint();
    high=n-1;
    low=0;
    mid=(high+low)/2;
    while(a[mid]!=x && low < high)
    {
        mid=(high+low)/2;
        if(x<a[mid])
            high=mid-1;
            else
            {
                low = mid +1;
            }
       
    }

     if(x==a[mid])
        putint(x);
        else
        {
            x = 0;
            putint(x);
        }
    
    x = 10;
    putch(x);

    return 0;
}