MEGChai MEGChai
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
首页 › 数据结构与算法 › 在线评测 › UVaOJ 10474 - Where is the Marble?
AOAPC II

UVaOJ 10474 - Where is the Marble?

Chai
2021-11-24 0:00:00在线评测阅读 413

问题描述

p10474

原题链接:UVaOJ 10474 - Where is the Marble?

相关说明:本题为《算法竞赛入门经典(第2版)》例题 5-1

解法一:排序

水题,直接排序后找满足条件的索引即可,注意从索引值 1 开始计数。

C++
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;
}
Python
1
#TODO
AOAPC II UVaOJ
赞赏 赞(0)
订阅
提醒
guest
guest
0 评论
内嵌评论
查看所有评论
  • 0
  • 0
Copyright © 2020-2023 MEGChai.
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
# 生活 # # 心理 # # 编程 # # 音乐 # # 写作 #
Chai
95
文章
4
评论
58
喜欢
wpDiscuz