哈工大机试 找x Easy *注意数组最大定义的问题

基本思想:

可以进行hash处理,也可以进行O(n)的遍历访问;

关键点:

这里有一个数组的最大定义问题,取决于机器,个人感觉1000 0000应该够用,但是还是保险点尽量再没给数据规模的时候少用一点;

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 0x7ffffff;

int thash[maxn];

int main() {
    int n;
    fill(thash, thash + maxn, -1);
    while (cin>>n){
        int a;
        for (int i = 0; i < n; i++) {
            cin >> a;
            thash[a] = i;
        }
        if (a <= 0) {
            cout << -1 << endl;
            continue;
        }
        cin >> a;
        cout << thash[a];
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12404481.html