【Vector】UVA 10474 Where is the Marble?

BabbleDay posted @ 2013年4月20日 14:52 in 刷题防身 , 793 阅读

题目不难,先排序再查找,对C++语言做点小结——Vector的使用

1. #include <vector>
2. vector<int> v;
3. v.clear();
4. v.resize(int n); 将vector的大小重置为n
5. v[int i] 可通过索引i直接获取内容
6. sort(a_vector.begin(), a_vector.end()) 调用函数排序,需要algorithm包 

#include <iostream>
#include <algorithm> //包含sort(a_vector.begin(), a_vector.end())
#include <cstring>
#include <vector>

using namespace std;

vector<int> v;
int number[10001];

void the_case(int N, int Q)
{
    v.clear();
    memset(number, 0, sizeof(number));
    v.resize(N);
    for(int i=0; i<N; i++)
    {
        cin >> v[i]; 
    }
    sort(v.begin(), v.end());
    for(int i=0; i<N; i++)
    {
        if(number[v[i]]==0) number[v[i]]=i+1;
    }
    while(Q--)
    {
        int t;
        cin >> t;
        if(number[t]==0) cout << t << " not found\n";
        else cout << t << " found at " << number[t] << endl;
    }
}

int main()
{
    int c=1;
    int N, Q;
    while(true)
    {
        cin >> N >> Q;
        if(N==0 && Q==0) break;
        cout << "CASE# " << c << ":\n";
        the_case(N,Q);
        c++;
    }
    return 0;
}
Avatar_small
Maha Board 7th Clas 说:
2023年9月24日 18:29

Maha Board High School Students Summers Holidays Don’t Waste Time 7th Standard Pursuing Students can Download the Textbooks and take the hard copy for further use, we will update the Latest Information on this page. Maharashtra e-Books 2024 Latest Version to be Upload Every Year in Online Mode.Maharashtra Board High School Textbooks 2024 are Very Important to Students on the Maha Board 7th Class Books 2024 Preparations of the Monthly and Final Examination. Here, we are Providing the Latest Edition of MH Class Book 2024 which is Published by the Balbharati. All the chapters can be Downloaded in form of PDFs.Maharashtra High School Students Follows.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter