HDOJ 1031-Design T-Shirt(排序)

    本题就是一道简单的排序,首先有n个人对m件T-shirt进行投票,从中选出得分高的k件,当出现相同的分时,选择编号靠前的。
    代码如下:
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <stack>
#include <cstring>
#include <fstream>
#include <cmath>

using namespace std;

class Data
{
    public:
        int num;
        double total;
};

int order[1005];
Data data[1005];

int cmp(Data a, Data b)
{
    if(a.total == b.total)
    {
        return a.num < b.num;
    }
    return a.total > b.total;
}

int main()
{
    //freopen("data.in", "r", stdin);
    int n, m, k;
    while(scanf("%d%d%d", &n, &m, &k) != EOF)
    {
        for(int j = 0; j < m; j ++)
        {
            data[j].num = j+1;
            data[j].total = 0;
            //一定要初始化,第一次Wa就是没有初始化,结果不知道改了其他地方,
        }
        for (int i = 0; i < n; i ++)
        {
            for (int j = 0; j < m; j ++)
            {
                double temp;
                scanf("%lf", &temp);
                data[j].total += temp;
            }
        }
        sort(data, data + m, cmp);

        for(int i = 0; i < k; i ++)
        {
            order[i] = data[i].num;
        }

        sort(order, order + k);
        for(int i = k - 1; i >= 0; i --)
        {
            if(i != k - 1)
            {
                printf(" ");
            }
            printf("%d", order[i]);
        }
        printf("
");
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/topk/p/6580118.html