UVA

来源:互联网 发布:大数据培训 达内就业 编辑:程序博客网 时间:2024/06/05 04:45

题目大意:给出行李箱的尺寸,小箱子能装在大箱子里,问装完后最少有几个箱子,并且每份箱子个数最少,并输出出组合方式。
解题思路:显然相同大小的行李箱必须分开放,所以最后箱子的个数就是重复出现最多次的尺寸。要实现每份箱子个数最少均分即可,就每隔这个数字输出,保证相同尺寸的行李箱不会组合在一起。

#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<string.h>using namespace std;int a[10010];bool cmp(int a, int b) {    return a < b;}bool flag = false;int main() {    int n;    while (scanf("%d", &n) && n != 0) {        for (int i = 0; i < n; i++) {            scanf("%d", &a[i]);        }        sort(a, a+n, cmp);        int tag = a[0];        int maxn = 0, cnt = 0;        for (int i = 0; i < n; i++) {            if (tag == a[i]) {                cnt++;            }            if (tag != a[i]) {                cnt = 1;                tag = a[i];            }            if (cnt > maxn) maxn = cnt;        }        if (flag) printf("\n");        flag = true;        printf("%d\n", maxn);        for (int i = 0; i < maxn; i++) {            printf("%d", a[i]);            for (int j = i+maxn; j < n; j += maxn) {                printf(" %d", a[j]);            }            printf("\n");        }    }return 0;}