poj 3312 水题

来源:互联网 发布:linux软件下载 编辑:程序博客网 时间:2024/06/08 18:36

这题非常水,但是题目比较长。

大意就是给n个人的名字,能不能将n个人分成k个队,使每个队中人名的长度与队中名字长度的平均值差不超过2.n一定能被k整除。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace std;char s[100];int ln[1005];int main() {    int n, k, i, j, ks = 1;    while(~scanf("%d%d", &n, &k) && (n || k)) {        for(i = 0; i < n; i++) {            scanf("%s", s);            ln[i] = strlen(s);        }        sort(ln, ln + n);        int m = n / k;        int is = 1;        for(i = 0; i < n; i += k) {            int sum = 0, mx = 00, mn = 100;            for(j = i; j < i + k; j++) {                sum += ln[j];                if(ln[j] < mn)                    mn = ln[j];                if(ln[j] > mx)                    mx = ln[j];            }            if(sum > (mn + 2) * k || sum < (mx - 2) * k)                is = 0;        }        if(ks != 1)            printf("\n");        printf("Case %d: %s\n", ks++, is ? "yes" : "no");    }    return 0;}


0 0
原创粉丝点击