7-5 二分查找 (15 分)

7-5 二分查找 (15 分)

利用二分查找找出所给出的数在数组中的下标

输入格式:

第一行输入n和m表示数组有n个数据,m表示要对m个数进行查找

输出格式:

所有输出在一行完成,行末没有多余空格和多余回车。

输入样例:

5 5
1 2 3 4 5
1 2 3 4 5```

### 输出样例:
```out
0 1 2 3 4结尾无空行

代码:

//超时问题
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    scanf("%d %d", &n, &m);
    map<int, int> mp;
    bool flag = false;
    for (int i = 0; i < n; i++)
    {
        int temp;
        scanf("%d", &temp);
        mp[temp] = i;
    }
    for (int i = 0; i < m; i++)
    {
        int temp;
        scanf("%d", &temp);
        if (!flag)
        {
            printf("%d", mp[temp]);
            flag = true;
        }
        else
        {
            printf(" %d", mp[temp]);
        }
    }
    return 0;
}

版权声明:本文为segegse原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>