HDU 1031 Design T-Shirt(水~)

来源:互联网 发布:ecs 搭建聊天室 python 编辑:程序博客网 时间:2024/06/08 15:25

Description
给出n个人对m件T恤的满意度,按编号降序输出这m件T恤中综合满意度前k大的
Input
多组输入,每组用例第一行为三个整数n,m,k,之后一个n*m矩阵表示n个人对m件T恤的满意度,以文件尾结束输入
Output
对于每组用例,输出综合满意度前k大的(按编号降序输出)
Sample Input
3 6 4
2 2.5 5 1 3 4
5 1 3.5 2 2 2
1 1 1 1 1 10
3 3 2
1 2 3
2 3 1
3 1 2
Sample Output
6 5 3 1
2 1
Solution
简单结构体排序题,用struct{pos,val}表示一种T恤,pos表示其编号,val表示其满意度综合,首先按val降序排序,然后对前k项按pos降序排序后输出即可
Code

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct node{    int pos;    double val;}t[1111];int cmp1(node a,node b){    return a.val>b.val;}int cmp2(node a,node b){    return a.pos>b.pos;}int main(){    int n,m,k;    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        memset(t,0,sizeof(t));//初始化         for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)            {                t[j].pos=j;//记录T恤编号                 double d;                scanf("%lf",&d);                t[j].val+=d;//累加满意度             }        sort(t+1,t+m+1,cmp1);//对所有T恤按满意度降序排         sort(t+1,t+k+1,cmp2);//对满意度前k大的T恤按编号降序排         for(int i=1;i<=k;i++)            printf("%d%c",t[i].pos,i==k?'\n':' ');    }    return 0;}
0 0
原创粉丝点击