一道关于奥运奖牌排序的题

一道关于奥运奖牌排序的题:


忘记在哪里看到的思路:若排序有多重优先级,则从低优先级到高优先级依次排序,排完了也就是正确结果。

发现C++真是个好东西。

下面是我写的程序,没有各种非法情况的检测。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
//#include <>
using namespace std;
struct olympic_honor
{
	char country[40];
	int gold;
	int silver;
	int tong;
};

bool more_gold(const struct olympic_honor &oh1,const struct olympic_honor &oh2)
{
	return oh1.gold > oh2.gold;
}
bool more_silver(const struct olympic_honor &oh1,const struct olympic_honor &oh2)
{
	return oh1.silver > oh2.silver;
}
bool more_tong(const struct olympic_honor &oh1,const struct olympic_honor &oh2)
{
	return oh1.tong > oh2.tong;
}
bool more_country(const struct olympic_honor &oh1,const struct olympic_honor &oh2)
{
	if(strcmp(oh1.country,oh2.country)<0)
		return true;
	else 
		return false;
}

int main()
{
	int num_country = 0;
	olympic_honor oh;
	oh.country[0] = '\0';
	oh.gold = 0;
	oh.silver = 0;
	oh.tong = 0;
	vector<olympic_honor> vec_oh; 
	printf("Input number: \n");
	scanf("%d",&num_country);
	printf("input %d countries: \n",num_country);
	for(int i = 0; i < num_country; i++)
	{
		scanf("%s%d%d%d",(oh.country),&(oh.gold),&(oh.silver),&(oh.tong));
		vec_oh.push_back(oh);
	}
	sort(vec_oh.begin(),vec_oh.end(),more_country);
	sort(vec_oh.begin(),vec_oh.end(),more_tong);
	sort(vec_oh.begin(),vec_oh.end(),more_silver);
	sort(vec_oh.begin(),vec_oh.end(),more_gold);
	for(size_t i = 0; i < vec_oh.size(); i++)
	{
		printf("%s\n",vec_oh[i].country);
	}
	system("pause");
	return 0;
}

sort(begin(),end(),规则);





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值