set的使用
1680: 查找1
Time Limit: 1 Sec Memory Limit: 128 MB
[Submit][Status][Web Board]
Description
給你一個長度是n的序列A,然后,有m次詢問,每次詢問是一個數(shù)字X,請你告訴我X在序列A中是否存在,存在輸出YES,否則輸出NO
Input
第一行 ,n,m,(n,m<=100000)
第二行n個數(shù)(每個數(shù)<=1000)
第三行m個數(shù)
Output
輸出答案
Sample Input
5 4
2 5 4 3 5
2 5 8 9
Sample Output
YES
YES
NO
NO
HINT
Source
AC代碼:
#include <stdio.h> #include <set> using namespace std; int main() {int n,m;while(~scanf("%d%d",&n,&m)){set<int>date;int x,y;for(int i = 0; i < n; i++){scanf("%d",&x);date.insert(x);}while(m--){scanf("%d",&y);if(date.count(y))printf("YES\n");elseprintf("NO\n");}}return 0; }/*
總結(jié):
set是STL中的一種容器,輸入的相同的數(shù)據(jù)只會保留一個
(即容器中保留的不會有兩個以上相同數(shù)據(jù))
而且輸入進去后會自動從小到大排序
1.頭文件:
2.定義:
set<數(shù)據(jù)類型>名字
3.插入數(shù)據(jù):
比如插入數(shù)據(jù)x
名字.insert(x);
4.常用于查找某個值是否存在
名字.count(x);
存在會返回1,否則0;
*/
總結(jié)
- 上一篇: 约瑟夫环问题(vector模拟过程)
- 下一篇: upper_bound()与lower_