hdoj 1304 a contesting decision(sort 函数 解决按结构体成员排序)

思路:用sort函数按每个队伍的penalty points 和 Slove 进行排序,输出解决最多的,并且penalty points 最小的

代码如下:

#include <iostream>  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
struct ss
{
	char name[20];
	int p1s, p1t, p2s, p2t, p3s, p3t, p4s, p4t;
	int pp;
	int slove;
}team[50];
bool cmp(struct ss a, struct ss b)
{
	if (a.slove == b.slove)
		return a.pp < b.pp;
	else
		return a.slove > b.slove;
}
int main()
{
	int t;
	int x;

	cin >> t;
	x = 0;
	
	while (t--)
	{
		scanf("%s%d%d%d%d%d%d%d%d", team[x].name, &team[x].p1s, &team[x].p1t, &team[x].p2s, &team[x].p2t, &team[x].p3s, &team[x].p3t, &team[x].p4s, &team[x].p4t );
		if (team[x].p1t)
		{
			team[x].pp+= (team[x].p1s - 1) * 20 + team[x].p1t;
			team[x].slove++;
		}
		if (team[x].p2t)
		{
			team[x].pp += (team[x].p2s - 1) * 20 + team[x].p2t;
			team[x].slove++;
		}
		if (team[x].p3t)
		{
			team[x].pp += (team[x].p3s - 1) * 20 + team[x].p3t;
			team[x].slove++;
		}
		if (team[x].p4t)
		{
			team[x].pp += (team[x].p4s - 1) * 20 + team[x].p4t;
			team[x].slove++;
		}
		x++;
	}
	sort(team, team + x, cmp);
	printf("%s %d %d\n", team[0].name, team[0].slove, team[0].pp);
	
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值