發表文章

目前顯示的是 1月, 2024的文章

e541. 10474 - Where is the marble

e541. 10474 - Where is the marble |  題目連結 :  https://zerojudge.tw/ShowProblem?problemid=e541 |  解題思路 : 用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