问题 J: 2013J-阅读2-二分-左边界

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:19 解决:15

题目描述


#include <iostream>  
using namespace std;  
int main() {  
    const int SIZE = 100;  
    int n, f, i, left, right, middle, a[SIZE];  
    cin>>n>>f;  
    for (i = 1; i <= n; i++)  
        cin>>a[i];  
    left = 1;  
    right = n;  
    do {  
        middle = (left + right) / 2;  
        if (f <= a[middle])  
            right = middle;  
        else  
            left = middle + 1;  
    } while (left < right);  
    cout<<left<<endl;  
    return 0;  
}  
输入:
12 17
2 4 6 9 11 15 17 18 19 20 21 25