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("\n");
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值