已知一个升序排列的整数数组,给定一个目标值,编写一个函数实现在数组中查找目标值的功能。如果找到目标值,返回它在数组中的下标,否则返回 -1。 定义数组arr[5],系统会输入5个数,输入目标数字aid。在数组中找到aid的下表,比如第一个位置就是0; 用系统提供的scanf输入6个数字,前5个为数组元素 arr[5],最后一个为目标数字 用二分法查找目标数据,实现int binary_search(int *arr, int len, int target); 最后打印printf("the position is %d", offset);(注意由于机器阅卷打印内容必须和左边打印格式一样,不要多也不要少任何东西,%d前是空格) int main() { int arr[5], target, position; while (scanf("%d %d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4], &target) != EOF) { position = binary_search(arr,5,target); printf("the position is %d",position); }
输入描述:
一个int型数组5个数字,和一个目标int型数字        


输出描述:
目标数字的数组中下标或者-1
示例1

输入

1 3 5 7 9 5

输出

the position is 2

说明

前面5个是数组,后面的5是目标值
加载中...