一道关于奥运奖牌排序的题:
忘记在哪里看到的思路:若排序有多重优先级,则从低优先级到高优先级依次排序,排完了也就是正确结果。
发现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(),规则);