【poj解题】3663

排序,

遍历,需要裁减

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 20000

int cows[MAX];
int N;
int L;

int cmp(const void * a, const void * b) {
    return *(int *)a - *(int *)b;    
}

int main() {
    int i;
    int j, k;
    int count;
    count = 0;
    scanf("%d%d", &N, &L);
    for(i = 0; i < N; i++) {
        scanf("%d", &cows[i]);   
    }
    qsort(cows, N, sizeof(cows[0]), cmp);  
    for(j = 0; j < N; j++) {
        if(cows[j] > L) {
            break;     
        }
        for(k = j + 1; k < N; k++) {
            if(cows[k] > L) {
                break;     
            } 
            if(cows[j] + cows[k] <= L) {
                count ++;     
            }
            else {
                break;     
            }
        }     
    } 
    printf("%d\n", count);
    return 0;
}

  

posted @ 2014-01-27 15:24 ~嘉言懿行~~我是煲仔饭~~ 阅读(...) 评论(...) 编辑 收藏