问题描述
p10474原题链接:UVaOJ 10474 - Where is the Marble?
相关说明:本题为《算法竞赛入门经典(第2版)》例题 5-1
解法一:排序
水题,直接排序后找满足条件的索引即可,注意从索引值 1 开始计数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <bits/stdc++.h> using namespace std; int main() { int n, q, query, num_case = 0; while (cin >> n >> q && (n && q)) { cout << "CASE# " << ++num_case << ":" << endl; vector<int> nums(n, 0); for (int i = 0; i < n; ++i) cin >> nums[i]; sort(nums.begin(), nums.end()); for (int i = 0; i < q; ++i) { cin >> query; auto positon = find(nums.begin(), nums.end(), query); if (positon != nums.end()) cout << query << " found at " << positon - nums.begin() + 1 << endl; else cout << query << " not found" << endl; } } return 0; } |
1 |
#TODO |