10474 - Where is the Marble? (UVA)

题目链接如下:

Online Judge

终于有一道连我都觉得很水的题……

网上大部分是用sort + lower_bound做(跟刘汝佳书上一样),我感觉find函数好用多了呀…还有一个比较少见的方法是桶排序:如下,也挺好使。UVA - 10474 - Where is the Marble? (基数排序)_raju and meena-CSDN博客

我的代码如下:

#include 
#include 
#include 
// #define debug

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    int n, q, k, kase = 0;
    while(scanf("%d %d", &n, &q) == 2 && n && q){
        std::vector vec(n);
        for(int i = 0; i < n; ++i){
            scanf("%d", &vec[i]);
        }
        sort(vec.begin(), vec.end());
        printf("CASE# %d:\n", ++kase);
        for(int i = 0; i < q; ++i){
            scanf("%d", &k);
            auto result = find(vec.begin(), vec.end(), k);
            if(result == vec.end()){
                printf("%d not found\n", k);
            } else{
                printf("%d found at %d\n", k, result - vec.begin() + 1);
            }
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,算法)