D - Where is the Marble? (UVA - 10474)

Posted Alpacaddhh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D - Where is the Marble? (UVA - 10474)相关的知识,希望对你有一定的参考价值。

- 题目大意

     题目中给出一个n个数的序列和q次查询,每次询问查询值是否在序列内,如果在,输出序列升序排列后的位置(从1开始)。

- 解题思路

     先使用sort()函数进行升序排列,然后枚举出其中与查找的值相同的数,符合条件就输出。

- 代码

#include<iostream>
#include<algorithm>

using namespace std;
int num[10000];
int main()
{
	
	int N, Q,q;
	int a=1,b=-1;

	while (cin>>N>>Q)
	{
		if (N == 0 && Q == 0)
			break;
		cout << "CASE# " << a << ":" << endl;
		for (int i = 0; i < N; i++)
		{
			cin >> num[i];
		}
		sort(num,num+N);
		for (int j = 0; j < Q; j++)
		{
			cin >> q;
			for (int k = 0; k < N; k++)
			{
				if (q == num[k])
				{
					cout << q << " found at " << k + 1 << endl;
					b = 0;
					break;
				}
			}
			 if (b == -1)
			 {
				 cout << q << " not found" << endl;
			 }
			 else
			 {
				 b = -1;
			 }
		}
		a++;
	}
	
	return 0;
	
}

  

以上是关于D - Where is the Marble? (UVA - 10474)的主要内容,如果未能解决你的问题,请参考以下文章

Where is the Marble?

10474 - Where is the Marble?(模拟)

UVA - 10474 Where is the Marble?(排序)

UVA10474 Where is the Marble?排序

Where is the Marble?(sort+lower_bound使用)

Where is the Marble? (algorithm)