刚入门编程,不知道哪里错了,求大佬帮我看看

P2249 【深基13.例1】查找

amtuilasi @ 2023-12-05 14:52:34

#include<iostream>
using namespace std;

long long arr[1000005];
long long abb[10000];
int n, m;
void init()
{

    cin >> n >> m;
    for (long long i = 1; i <= n; i++)
    {
        cin >> arr[i];
    }
}

long long binsort(long long arr[],long long begin,long long end,long long key)
{
    if (end <= begin)return begin;
    long long mid = ((end + begin)/2);
    if (arr[mid] < key)
    {
        begin = mid+1;
    }
    else if (arr[mid] > key)
    {
        end = mid;
    }
    else
    {
        end = mid;
    }
    return binsort(arr, begin, end, key);
}

int main()
{
    init();

    for (long long i = 1; i <= m; i++)
    {
        cin >> abb[i];
    }
    for (long long i = 1; i <= m; i++)
    {
        long long temp = binsort(arr, 1, n, abb[i]);
        if (arr[temp] == abb[i])
        {
            cout << temp << " ";
        }
        else
        {
            cout << -1 <<" ";
        }

    }

    return 0;
}

|