uva 10474 - Where is the Marble?

题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=1415

 

题目比较简单,先排序,再搜索。。

/*
 * uva10474.cpp
 *
 *  Created on: 2013-4-11
 *      Author: kevinjiang
 */
#include<cstdio>
#include<cstdlib>

const int MAX=10005;

int nums[MAX];
int queries[MAX];

int cmp(const void*a,const void*b){

	return (*(int *)a-*(int *)b);
}


int main()
{
//	setbuf(stdout,NULL);
	int counter=1;
	int n,q;
	while(scanf("%d%d",&n,&q))
	{
		if(n==0&&q==0)
			break;

		for(int i =0;i<n;i++)
			scanf("%d",&nums[i]);

		for(int i=0;i<q;i++)
			scanf("%d",&queries[i]);

		qsort(nums,n,sizeof(nums[0]),cmp);

		printf("CASE# %d:\n",counter++);

		for(int i=0;i<q;i++)
		{
			int index=-1;
			for( int j=0;j<n;j++)
			{
				if(nums[j]==queries[i])
				{
					index=j+1;
					break;
				}
			}

			if(index!=-1){
				printf("%d found at %d\n",queries[i],index);
			}
			else
				printf("%d not found\n",queries[i]);

		}

	}

	return 0;
}


 

你可能感兴趣的:(where)