标签:style blog color io os ar for sp div
6 4 23 34 46 768 343 343 2 4 23 343
NO NO YES YES
思路:首先用一个一位数组存入元素,然后运用快速排序进行排序,最后二分法查找
 
#include<stdio.h>
#include<stdlib.h>
int s[1000005];
int low,high;
int cmp(const void *a,const void *b)//快速排序
{
	return (*(int *)a-*(int *)b);
}
int jisuan(int x)//二分法查找
{
   int mid;
   while(low<=high)
   {
        mid=(low+high)/2;
		if(s[mid]==x)
			return 1;
		else if(s[mid]<x)
			low=mid+1;
		else
			high=mid-1;
   }
   if(low>high)
	   return 0;
}
int main()
{
    int i,m,n,a;
	scanf("%d%d",&m,&n);
	for(i=0;i<m;i++)
		scanf("%d",&s[i]);
	qsort(s,m,sizeof(int),cmp);//快速排序
    while(n--)
	{
		low=0;high=m-1;
		scanf("%d",&a);
		if(jisuan(a)==1)//二分法
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}        标签:style blog color io os ar for sp div
原文地址:http://blog.csdn.net/u013238646/article/details/40021727