lower_bound的簡單用法


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1000;
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);
        while(q--)
        {
            scanf("%d",&x);
            int p=lower_bound(a,a+n,x)-a;//lower_bound查找大於或等於x的第一個位置
            if(a[p]==x)
            printf("%d found at %d\n",x,p+1);
            else
            printf("%d not found\n",x);
        }
    }
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM