zoukankan      html  css  js  c++  java
  • Problem A Where is the Marble?(查找排序)

    题目链接:Problem A

    题意:有n块大理石,每个大理石上写着一个非负数,首先把数从小到大排序,接下来有Q个问题,每个问题是是否有某个大理石上写着x,如果有,则输出对应的大理石编号。

    思路:先排序,然后实现查找某个数第一次出现的位置。

    note:
    头文件:#include <algorithm>
    ForwardIter lower_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于等于val的位置。
    ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置。

    code:

     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 const int MAXN = 10005;
     5 int main()
     6 {
     7     int n, m, x, cnt = 0;
     8     int a[MAXN];
     9     while (scanf("%d %d", &n, &m), n + m)
    10     {
    11         for (int i = 0; i < n; ++i) scanf("%d", a + i);
    12         sort(a, a + n);
    13         printf("CASE# %d:
    ", ++cnt);
    14         while (m--)
    15         {
    16             scanf("%d", &x);
    17             int p = lower_bound(a, a + n, x) - a;
    18             if (a[p] == x) printf("%d found at %d
    ", x, p + 1);
    19             else printf("%d not found
    ", x);
    20         }
    21     }
    22     return 0;
    23 }
  • 相关阅读:
    Git Bash关键命令
    一个不需要Log4Net的写日志的简单方法
    未知软件
    Linux
    Linux
    Linux
    Linux
    Linux
    Linux
    Linux
  • 原文地址:https://www.cnblogs.com/ykzou/p/4610481.html
Copyright © 2011-2022 走看看