3990. 二分查找

Easy函数一维数组二分查找

时间限制:2000 ms

内存限制:512 MiB

题面

Write a function binarySearch(a, n, x) that searches xx integer array aa of length nn, and returns the index of aa (1-1 if xx can not be found in aa).

Assume elements of aa are entered in descending order, and the value of evey element is unique.

Write a program to call binarySearch(a, n, x).

/***************************************************************/
/*                                                             */
/*  DON'T MODIFY main function ANYWAY!                         */
/*                                                             */
/***************************************************************/

#include <stdio.h>

int binarySearch(int a[], int n, int x) {
    // TODO: your function definition
}

/***************************************************************/
#define N 100000
int a[N];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    //********** binarySearch is called here *************
    int q, x;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        scanf("%d", &x);
        printf("%d\n", binarySearch(a, n, x));
    }
    //**************************************************
    return 0;
}

提示

Note that the function binarySearch(a, n, x) will be called many times in the test.