大理石在哪(Where is the Marble?,UVa 10474)

 STL专题之一:

1.学会使用algorithm头文件中的sort和lower_bound;

2.sort(a,a+n)排序,a为数组名

3.lower_bound的作用是查找"大于或者等于x的第一个位置"——int p= lower_bound(a,a+n,x)-a;

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<set>
const int maxn = 10000;
using namespace std;
int n,q;
int a[maxn];
int main()
{
	int cas=0,x;
	while(cin>>n>>q)
	{
        if(n==0&&q==0) break;
		printf("CASE# %d:\n", ++cas);
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			}
		sort(a,a+n);//排序
		while(q--)
		{
			cin>>x;
			int p=lower_bound(a,a+n,x)-a;//查找位置
			if(a[p]==x) printf("%d found at %d\n",x,p+1);
			else printf("%d not found\n",x); 
			}	
	}
	return 0;	
 } 

猜你喜欢

转载自blog.csdn.net/csustudent007/article/details/81235776
今日推荐