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;

}

  

原文地址:https://www.cnblogs.com/alpacadh/p/8438500.html

时间: 2024-11-11 18:31:33

D - Where is the Marble? (UVA - 10474)的相关文章

A - Where is the Marble? UVA - 10474

ACM紫书 第五章 P108 [排序与检索] 题意:找输入的数在排完序之后的位置. 想自己用vector写下,却报错 iterator cannot convert '__gnu_cxx::__normal<int*, std::vector<int> >' to 'int' in assignment ·迭代器的接口几乎相当于普通的指针.让一个迭代器递增只需调用++操作符. 使用*操作符可以得到迭代器引用的数据值.因而迭代器可以被任为是一种智能指针 lower_bound ()返

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

题目描述:算法竞赛入门经典例题5-1 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int maxn = 10000 ; 5 int main() 6 { 7 int n,q,a[maxn] ,k=0; 8 while(scanf("%d%d",&n,&q)==2 && n &&q){ 9 for(int i=0;i<

UVA 10474:Where is the Marble?(STL初步)

 Where is the Marble?  Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Mee

uva 10474 Where is the Marble?(排序)

uva 10474 Where is the Marble? Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them.

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

UVA - 10474 Where is the Marble? Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginni

并查集(UVA 1106)

POINT: 把每个元素看成顶点,则一个简单化合物就是一条无向边,若存在环(即k对组合中有k种元素),则危险,不应该装箱,反之,装箱: 用一个并查集维护连通分量集合,每次得到一种化合物(x, y)时检查x, y是否在同一集合中,如果是,拒绝,反之接受. 并查集 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的元素所在的集合合并. -----------

逆向+两次bfs(UVA 1599)

为什么都说简单好想咧.坦白从宽看了人家的代码,涨了好多姿势,, http://blog.csdn.net/u013382399/article/details/38227917 被一个细节坑了.. 2147483647是0x7fffffff啊啊啊,7个f!!! 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <c

Carmichael Numbers(Uva 10006)

  Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography. Alvaro is one

F - Andy&#39;s First Dictionary (UVA - 10815)

- 题目大意 给出一段文章,然后将其中不重复的单词拿出来(全为小写并且由a-z的顺序). - 解题思路 我在网上看到的一个stringstream函数,它除了进行字符串和数字转换之外的另一个用途:分割单词.将字符串中所有非单词字符全部转换为空格,然后再用代码中的方法分割出单词.有这种方法就好做多了.结合set容器即可. - 代码 #include<iostream> #include<set> #include<string> #include<sstream&g