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 ()返回值是地址,要用迭代器操作;

还有就是lower_bound返回的是第一个大于等于对象的地址

不一定是等于啊,要判断在不在 数组里,还要再判断一下

还有就是 每次循环一定要初始化vector,进行操作 s.clear()

贴代码(第一次用c++的输入输出格式<<>>总打反)

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

vector<int> s;
int main(){
    int rnd=0,n,qst,q,x;
    while(++rnd){
            s.clear();
        cin>>n>>qst;
        if(n==0&&qst==0)break;
        cout<<"CASE# "<<rnd<<":"<<endl;
        while(n--){
            cin>>x;
            s.push_back(x);
        }
        sort(s.begin(),s.end());
        while(qst--){
            cin>>q;
            vector<int>::iterator p=lower_bound(s.begin(),s.end(),q);
            if(*p==q)
            cout<<q<<" found at "<<p-s.begin()+1<<endl;
            else
            cout<<q<<" not found"<<endl;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/-ifrush/p/10159681.html

时间: 2024-10-31 22:01:37

A - Where is the Marble? UVA - 10474的相关文章

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 (c

大理石在哪儿 (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?(排序)

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 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?

Where is the Marble?  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 writ

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

Where is the Marble? Descriptions: 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 th

uva 10474 Where is the Marble?(简单题)

我很奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了,wa了两次,我还很不解的怀疑了为什么会 wa,原来是我竟然把要找的数字也排序了,当时只是想着能快一点查找,所以就给他排序了,没考虑到要按给的顺序输 出答案,这次真是二了,,,看别人题解有用打表做的,那个应该是正确解法,我的耗时980ms,估计数据再大一些就 要TLE了 贴代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int cmp(

uva 10474 Where is the Marble?[ vector ]

思路:sort+low_bound()二分 #include<vector> #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<set> using namespace std; int main() { vector<int>v; int n,m; int cas=1; while(scanf("