e541. 10474 - Where is the marble
e541. 10474 - Where is the marble
|  解題思路 : 用lower_bound尋找大於或等於key的第一個位置
|  程式碼 : 
/*
e541. 10474 - Where is the marble
https://zerojudge.tw/ShowProblem?problemid=e541
skyblue
AC (2ms, 72KB)
*/
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
    int n,q,kase = 0;
    while(scanf("%d%d", &n,&q) == 2 && n!=0){
        printf("CASE# %d:\n", ++kase);
        //read data
        int a[1000] {0};
        for(int i = 0; i<n; i++){
            scanf("%d", &a[i]);
        }
        //sort data
        sort(a,a+n);
        while(q--){
            int x;
            scanf("%d", &x);
            int p = lower_bound(a,a+n,x)-a;
            if(a[p] == x) printf("%d found at %d\n", x, p+1);
            else printf("%d not found\n", x);
        }
    }
    return 0;
}
/*
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
*/
留言
張貼留言