UVa 11292 The Dragon of Loowater 勇者斗恶龙

思维体操入门,进行排序后,拿最小的进行比对,如果可以砍头,将骑士能力值置为-1,并加上金额,否则遇到不行的时候,直接打印退出程序。

#include "iostream"
#include "algorithm"
using namespace std;
int n, m;
int head[99999], knight[99999]; // 头的直径
int main() {
    while(true) {
        scanf("%d%d", &n, &m);
        if(n == 0 && m == 0) break;
        for(int i = 0; i < n; i++) 
            scanf("%d", head + i);
        for(int i = 0; i < m; i++)
            scanf("%d", knight + i);
        sort(head, head + n);
        sort(knight, knight + m);
        int price = 0, i, j, hasPri= false;
        for(i = 0; i < n; i++) {
            bool cmpte = false;
            for(j = 0; j < m; j++) {
                if(knight[j] >= head[i]) {
                    price += knight[j];
                    knight[j] = -1;
                    cmpte = true;
                    break;
                }
            }
            if(!cmpte) {
                printf("Loowater is doomed!\n");
                hasPri = true;
                break;
            }
        }
        if(!hasPri) printf("%d\n", price);
    }
    return 0;
}

 

posted @ 2020-04-17 03:14  SteveYu  阅读(160)  评论(0编辑  收藏  举报