0pts 求调%%%

P2249 【深基13.例1】查找

wangjunwu2014 @ 2024-10-08 20:36:47

#include<iostream>
#define MAXN 1000010
using namespace std;
int a[MAXN], m, n, q;
int find(int x)
{
    int l = 1, r = n;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (a[mid] == x) return mid;
        else if (a[mid] > x) r = mid - 1;
        else l = mid + 1;
    }
    return -1;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 0; i < m; ++i)
    {
        cin >> q;
        cout << find(q) << " ";
    }
    return 0;
}

by imzfx_Square @ 2024-10-08 20:39:26

@wangjunwu2014 注意题目要求输出这个数字在序列中第一次出现的编号


by wangjunwu2014 @ 2024-10-08 20:40:21

@imzfx_Square 蟹蟹!


|