输入有多组数据。 每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m,n<=100)。
如果在n个数组中输出YES否则输出NO。
5 1 5 2 4 3 3 2 5 6
YES YES NO
//先记一个别的做法,这个空间换时间做的还是不错的,o(m)的时间,就是这个范围有点难确定
#include<bits/stdc++.h>
int main(){
int m,n,s;
while(scanf("%d",&n)!=EOF){
int a[1000]={0};
for(int i=0;i<n;i++){
scanf("%d",&m);
a[m]=1;
}
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&s);
if(a[s]==1) printf("YES\n");
else printf("NO\n");
}
}
}//hash法
//下面这个自己写的时间复杂度是o(m*n)因为感觉说了m,n的范围都在100以内,所以即使乘积也不会超限
#include<iostream>
using namespace std;
int main(){
int n,m;
while(cin>>n){
int *a=new int[n],b;
for(int i=0;i<n;i++)
cin>>a[i];
cin>>m;
for(int i=0;i<m;i++){
cin>>b;
bool judge=1;
for(int j=0;j<n&&judge;j++)
if(b==a[j]){
cout<<"YES"<<endl;
judge=0;
}
if(judge)
cout<<"NO"<<endl;
}
}
return 0;
} #include <iostream>
#include <algorithm>
using namespace std;
int main() {
int x[102];
int n, q, m;
while (cin >> n) {
for (int i = 0; i < n; i++) {
cin >> x[i];
}
sort(x, x+n);
cin >> m;
while (m--) {
cin >> q;
cout << ((*lower_bound(x, x+n, q) == q) ? "YES" : "NO") << endl;
}
}
}
#include <iostream>
using namespace std;
int main(){
int n,m;
while(cin>>n){
int i,a[100],b[100];
for(i=0;i<n;i++)
cin>>a[i];
cin>>m;
for(i=0;i<m;i++)
cin>>b[i];
for(i=0;i<m;i++){
bool flag=false;
for(int j=0;j<n;j++){
if(b[i]==a[j]){
flag=true;
break;
}
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int x,n,m,num[100];
map<int,int> M;
int main()
{
cin>>n;
for(int i =0;i<n;i++)
{
cin>>x;
M[x]++;
}
cin>>m;
for(int i=0;i<m;i++)
cin>>num[i];
for(int i =0;i<m;i++)
{
if(M[num[i]]==0)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;
}
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
while (in.hasNext()){
int n=in.nextInt();
Set<Integer> set=new HashSet<Integer>();
for (int i=0;i<n;i++)
set.add(in.nextInt());
n=in.nextInt();
for (int i=0;i<n;i++){
int a=in.nextInt();
if (set.add(a))
{
set.remove(a);
System.out.println("NO");
}
else
System.out.println("YES");
}
}
}
}
#include<iostream>
#include<vector>
using namespace std;
int main(void)
{
int n;
vector<int> a;
vector<int> b;
while(cin >> n)
{
for(int i = 0;i < n;i++)
{
int x;
cin >> x;
a.push_back(x);
}
int m;
cin >> m;
for(int i = 0;i < m;i++)
{
int y;
cin >> y;
b.push_back(y);
}
for(int i = 0;i < m;i++)
{
int count = 0;//标记是否有相同数字的个数
for(int j = 0;j < n;j++)
{
if(b[i] == a[j])
count++;
}
if(count == 0)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
return 0;
} #include<stdio.h>
#include<stdlib.h>
//比较函数
int cmp_inc(const void* a,const void* b){
return *(int *)a - *(int *)b;
}
//二分查找
int Binary_Search(int *arr,int n,int key){
int low=0;
int high=n-1;
int mid;
while(low<=high){
mid=(low+high)/2;
if(arr[mid]==key){
return mid;
}
else if(arr[mid]<key){
low=mid+1;
}
else{
high=mid-1;
}
}
return -1;
}
int main(){
int n,m;
int a[100];
int b[100];
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
qsort(a,n,sizeof(int),cmp_inc);
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&b[i]);
}
for(int i=0;i<m;i++){
int s=Binary_Search(a,n,b[i]);
if(s!=-1){
printf("YES\n");
}else{
printf("NO\n");
}
}
}
} #include <bits/stdc++.h>
using namespace std;
int a[101],b[101];
bool flag[101];
int main()
{
int n,m;
int i;
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
sort(a, a+n);
scanf("%d",&m);
for(i = 0; i < m; i++)
{
scanf("%d",&b[i]);
flag[i] = binary_search(a, a+n, b[i]);
}
for(i = 0; i < m;i++)
{
if(flag[i]) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
好像用STL好像没什么意思,反正思路是这样的。。
#include<bits/stdc++.h> //万能头
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int BinarySearch(int a[],int n,int b)
{
int left=0;
int right=n-1;
while(left<=right)
{
int middle=(left+right)/2;
if(a[middle]<b)
{
left=middle+1;
}
else if(b<a[middle])
{
right=middle-1;
}
else
{
return 1;
}
}
return 0;
}
int main(){
int m,n;
while(scanf("%d",&n)!=EOF)
{
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&m);
sort(a,a+n);
int b[m];
for(int i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
for(int i=0;i<m;i++)
{
if(BinarySearch(a,n,b[i] )==1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}
return 0;
} import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++) set.add(scanner.nextInt());
int m = scanner.nextInt();
for (int i = 0; i < m; i++) System.out.println(set.contains(scanner.nextInt())?"YES":"NO");
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
using namespace std;
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int num;
set<int> a;
for (int i = 0; i<n; i++){
scanf("%d", &num);
a.insert(num);
}
int m,c;
scanf("%d", &m);
for (int i = 0; i < m; i++){
scanf("%d", &c);
if(a.find(c)!=a.end()){
printf("YES\n");
}else{
printf("NO\n");
}
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1111;
int num[maxn];
int low_bound(int first, int last, int value)
{
while (first < last)
{
int mid = first + (last - first) / 2; // 防溢出
if (value > num[mid])
first = mid + 1;
else
last = mid;
}
return first;
}
int main()
{
// freopen("in.txt", "r", stdin);
int n, m;
while (~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
scanf("%d", &num[i]);
sort(num, num + n);
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
int value;
scanf("%d", &value);
int index = low_bound(0, n, value);
if (value == num[index])
puts("YES");
else
puts("NO");
}
}
return 0;
}
#include<stdio.h>
#include<algorithm>
using namespace std;
int solve(int a[],int left,int right,int k){
int mid;
while(left<right){
mid=(left+right)/2;
if(k<=a[mid])
right=mid;
else
left=mid+1;
}
return left;
}
int main(){
int n,a[100];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int m,b[100];
scanf("%d",&m);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
sort(a,a+n);
int j=0,r;
while(j<m){
r=solve(a,0,n-1,b[j]) ;
if(b[j]==a[r]){
printf("YES\n");
}
else
printf("NO\n");
j++;
}
return 0;
}
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n,m;
while(scanf("%d", &n)!=EOF){
int a[10000];
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
a[tmp] = 1;
}
cin>>m;
for(int i=0;i<m;i++){
int tmp;
cin>>tmp;
if(a[tmp]==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
}
#include <iostream>
using namespace std;
int main( )
{
int m,n;
while(cin>>n)
{
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
cin>>m;
int b[m];
for(int j=0;j<m;j++)
cin>>b[j];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(b[i]==a[j])
{
cout<<"YES"<<endl;
break;
}
if(j==n-1)
cout<<"NO"<<endl;
}
}
}
return 0;
}
//我理解错了,原来是要拿第二个数组中的每个数和第一个数组中的数字比较
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
//第一个数字,也是第一个数组的长度
int a1=sc.nextInt();
//定义第一个数组
int[] a=new int[a1];
for(int i=0;i<a.length;i++){
a[i]=sc.nextInt();
}
//输入第二个数组的长度
int b1=sc.nextInt();
int[] b=new int[b1];
for(int i=0;i<b.length;i++){
b[i]=sc.nextInt();
}
for(int i=0;i<b.length;i++){
test(b[i],a);
}
}
}
public static void test(int a1,int[] a){
boolean flag=false;
while(!flag){
for(int i=0;i<a.length;i++){
if(a[i]==a1){
System.out.println("YES");
flag=true;break;
}
}
break;
}
if(flag==false)
System.out.println("NO");
}
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int N;
while (cin >> N)
{
int num;
vector<int> vec;
for (int i = 0;i < N;++i)
{
cin >> num;
vec.push_back(num);
}
int n;
cin >> n;
for (int i = 0;i < n;++i)
{
cin >> num;
if (find(vec.begin(), vec.end(), num) != vec.end())
cout << "YES\n";
else
cout << "NO\n";
}
}
return 0;
}