1462: [蓝桥杯2019初赛]组队 【简单】

在这里插入图片描述
http://oj.ecustacm.cn/problem.php?id=1462

解法一: 直接看,可以轻松的得出答案。
在这里插入图片描述
解法二: 写代码.

思路就是用结构体。不过我写的代码还是有bug的。
当然对于当前的数据可以得到正确的答案。

bug 在于 假如一个人 他在1和3号位都很吊,当选一号位的时候如果把他选走了。
当选第三号位时 假如除了他其他的人都很菜,但是他已经选走了,那么从剩下的人中选,这显然不是最优的。
当然我也为了已经尽可能的减少错误,在结构体我还定义了一个sum来记录它们的和。
用和来表示它们的性价比,当选1号位时,如果有两个分数都是最高的,那么选性价比低的。
因为这样后面的选的也都是最好的。

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
struct student
{
	int id;
	int a1;
	int a2;
	int a3;
	int a4;
	int a5;
	int sum;//性价比
	bool flag;//表示有没有被选
}stu[25];
bool cmp1(student a, student b)
{
	if(a.a1==b.a1)
	{
		return a.sum<b.sum;
	}
	return a.a1>b.a1;
}
bool cmp2(student a, student b)
{
	if(a.a2==b.a2)
	{
		return a.sum<b.sum;
	}
	return a.a2>b.a2;
}
bool cmp3(student a, student b)
{
	if(a.a3==b.a3)
	{
		return a.sum<b.sum;
	}
	return a.a3>b.a3;
}
bool cmp4(student a, student b)
{
	if(a.a4==b.a4)
	{
		return a.sum<b.sum;
	}
	return a.a4>b.a4;
}
bool cmp5(student a, student b)
{
	if(a.a5==b.a5)
	{
		return a.sum<b.sum;
	}
	return a.a5>b.a5;
}
int main(void)
{
	FILE *p=fopen("1.txt","r");
	if(!p)
	{
		printf("文件打开失败了");
	}
	int i=0;
	while( fscanf(p,"%d %d %d %d %d %d",&stu[i].id,&stu[i].a1,&stu[i].a2,&stu[i].a3,&stu[i].a4,&stu[i].a5) != EOF )
	{
		stu[i].sum=stu[i].a1+stu[i].a2+stu[i].a3+stu[i].a4+stu[i].a5;
		//printf("%d %d %d %d %d %d\n",stu[i].id,stu[i].a1,stu[i].a2,stu[i].a3,stu[i].a4,stu[i].a5);
		i++;
	}
	int sum=0;
	
	sort(stu,stu+20,cmp1);
	sum+=stu[0].a1;
	stu[0].flag=true;
	
	sort(stu,stu+20,cmp2);
	for(int i=0;i<20;i++)
	{
		if(stu[i].flag==false)
		{
			sum+=stu[i].a2;
			stu[i].flag=true;
			break;
		}
	}
	
	sort(stu,stu+20,cmp3);
	for(int i=0;i<20;i++)
	{
		if(stu[i].flag==false)
		{
			sum+=stu[i].a3;
			stu[i].flag=true;
			break;
		}
	}
	
	sort(stu,stu+20,cmp4);
	for(int i=0;i<20;i++)
	{
		if(stu[i].flag==false)
		{
			sum+=stu[i].a4;
			stu[i].flag=true;
			break;
		}
	}
	
	sort(stu,stu+20,cmp5);
	for(int i=0;i<20;i++)
	{
		if(stu[i].flag==false)
		{
			sum+=stu[i].a5;
			stu[i].flag=true;
			break;
		}
	}
	printf("%d\n",sum);
	return 0;
} 

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值