例题5-1 大理石在哪儿(Where is the Marble?,UVa 10474)

原题链接:https://vjudge.net/problem/UVA-10474
分类:
备注:排序和查找
注意:如果没看紫书的话,要看到题目里的ascending才知道讲了什么东西。

代码如下:

#include
#include
using namespace std;
const int maxn = 10000 + 5;
int n, q, x, marble[maxn], kase;
int main(void)
{
	while (cin >> n >> q && n)
	{
		cout << "CASE# " << ++kase << ":\n";
		for (int i = 0; i < n; i++)
			cin >> marble[i];
		sort(marble, marble + n);
		for (int i = 0, flag = 0; i < q; i++)
		{
			cin >> x;
			for(int i=0;i<n;i++)
				if (marble[i] == x)
				{
					cout << x << " found at " << i+1 << "\n";
					flag = 1; break;
				}
			if (flag) { flag = 0; continue; }
			cout << x << " not found\n";
		}
	}
	return 0;
}

你可能感兴趣的:(《算法竞赛入门经典(第2版)》)