where is the marble?(uva 10474)
来源:互联网 发布:云计算按使用者分 编辑:程序博客网 时间:2024/05/21 10:51
题目大意:现有n个大理石,每个大理石上写了一个非负整数。首先把个数从小到大排序,然后回答q个问题。每个问题问是否有一个大理石上写着某个数x,如果是,还要回答那个大理石上写着x。
题目不难,就是对lower_bound(a,a + n,x)函数的一个应用,该函数返回的是大于或者等于x的第一个位置(地址)。
比如说:a[6] = {1, 2,2, 4,5 ,5}, lower_bound(a,a + 6,5)返回的是第一个4(也就是a[4] = 5)的地址。
#include<cstdio>#include<algorithm>using namespace std;#define maxn 1000int main(){ int n, q; while(scanf("%d %d", &n, &q) && n){ int kase = 0; int x, a[maxn]; printf("CASE# %d:\n",++kase); for(int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); while(q--){ scanf("%d", &x); int p = lower_bound(a, a + n, x) - a; if(a[p] == x) printf("%d fount at %d\n", x, p + 1); //这里注意,因为是数组,下标从0开始所以p加1 else printf("%d not found\n",x); } } return 0;}
阅读全文
0 0
- where is the marble?(uva 10474)
- UVa 10474 Where is the Marble?
- Uva 10474 Where is the Marble?
- uva 10474 Where is the Marble?
- UVA 10474 Where is the Marble?
- UVA 10474 - Where is the Marble
- uva-10474 - Where is the Marble?
- uva 10474 - Where is the Marble?
- UVA 10474 - Where is the Marble?
- UVa 10474 - Where is the Marble?
- UVa 10474 - Where is the Marble
- UVa 10474 - Where is the Marble?
- UVA 10474 Where is the Marble?
- UVA 10474 - Where is the Marble
- UVa - 10474 - Where is the Marble?
- uva 10474 Where is the Marble?
- uva 10474 Where is the Marble?
- uva - 10474 - Where is the Marble?
- ACM高斯消元法 亦或方程组求秩 (HDU5833 Zhu and 772002)
- 变态跳台阶
- 已知前序(先序)与中序输出后序
- 2017阿里巴巴在线测试题
- 类的带参方法
- where is the marble?(uva 10474)
- textview实现跑马灯效果
- 九大编程语言的优劣比较
- Quartz定时调度器01
- Effective Java慎用重载
- python学习------生成cdkey小程序
- c、c++、matlab、python读取文件、保存文件
- java基础1-认识Java
- 双调欧几里得旅行商问题 UVA 1347 Tour