Think:
STL中的set直接水过去~~~~
Problem Description
在一个的序列里,查找元素是否存在,若存在输出YES,不存在输出NO.
Input
本题多组数据,首先输入一个数字n,然后输入n(n<=1000)个数,然后再输入一个查找数字。
Output
若存在输出YES,不存在输出NO.
Example Input
4
1 3 5 8
3
Example Output
YES
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, i, m;
while(cin >> n)
{
set<int>s;
s.clear();
for (i = 0; i <= n - 1; i ++)
{
int key;
cin >> key;
s.insert(key);
}
cin >> m;
if (s.count(m) != 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}