UVA 10474 Where is the Marble
2016-05-25 18:31
295 查看
这是一道较为简单的匹配问题,先用sort函数对输入的数组进行排序,在通过查找元素,找到目标元素所在位置并输出
#include <cstdio> #include <algorithm> using namespace std; const int maxn = 10000; int main() { int n, q, x, a[maxn], kase = 0; while(scanf("%d%d", &n, &q) == 2 && n) { printf("CASE# %d:\n", ++kase); for(int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a+n);//包含于algorithm头文件里 while(q--) { scanf("%d", &x); int p = lower_bound(a, a+n, x) - a;//从a数组中,寻找第一个等于x的值 if(a[p] == x) printf("%d found at %d\n", x, p+1); else printf("%d not found\n", x); } } return 0; }
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?