1、查询信息
2022年北京冬奥会奖牌榜
奥运会奖牌榜排序规则
世界各国和地区名称代码
2、构建存储类型
struct MedalList
{
string CountryName,CountryName_cn;
int world_code;
int gold,silver,bronze,total;
};
构建一个MedalList,该结构体由英文国家名,中文国家名,国家世界编码,金银铜牌数量和总奖牌数量组成
3、获取数据并封装成get_data子函数(这里暂时先这样用,后面将用Jason文件格式代替)
MedalList medal_list[N]; //鍏ㄥ眬鍙橀噺
void get_data()
{
medal_list[0] = {"Norway \t","挪威 \t",578,16,8,13,37};
medal_list[1] = {"German \t","德国 \t",276,12,10,5,27};
medal_list[2] = {"China \t","中国 \t",156,9,4,2,15};
medal_list[3] = {"America ","美国 \t",840,8,10,7,25};
medal_list[4] = {"Sweden \t","瑞典 \t",752,8,5,5,18};
medal_list[5] = {"Netherlands","荷兰 \t",528,8,5,4,17};
medal_list[6] = {"Austia ","奥地利 \t",040,7,7,4,18};
medal_list[7] = {"Switzerland","瑞士 \t",756,7,2,5,14};
medal_list[8] = {"ROC \t","俄罗斯奥委会",643,6,12,14,32};
medal_list[9] = {"France ","法国 \t",250,5,7,2,14};
medal_list[10] = {"Canada ","加拿大 \t",124,4,8,14,26};
medal_list[11] = {"Japan \t","日本 \t",392,3,6,9,18};
medal_list[12] = {"Italy \t","意大利 \t",380,2,7,8,17};
medal_list[13] = {"Koria \t","韩国 \t",410,2,5,2,9};
medal_list[14] = {"Snownenia","斯洛文尼亚",705,2,3,2,7};
medal_list[15] = {"Finland ","芬兰 \t",246,2,2,4,8};
medal_list[16] = {"New Zealand","新西兰 \t",554,2,1,0,3};
medal_list[17] = {"Australia","澳大利亚 ",036,1,2,1,4};
medal_list[18] = {"UK \t","英国 \t",826,1,1,0,2};
medal_list[19] = {"Hungary ","匈牙利 \t",348,1,0,2,3};
medal_list[20] = {"Belgium ","比利时 \t",056,1,0,1,2};
medal_list[21] = {"Chech \t","捷克 \t",203,1,0,1,2};
medal_list[22] = {"Strova ","斯洛伐克 ",703,1,0,1,2};
medal_list[23] = {"Belarus ","白俄罗斯 ",112,0,2,0,2};
medal_list[24] = {"Spain \t","西班牙 \t",724,0,1,0,1};
medal_list[25] = {"Ukrain ","乌克兰 \t",804,0,1,0,1};
medal_list[26] = {"Esthonia ","爱沙尼亚 ",233,0,0,1,1};
medal_list[27] = {"Latvia \t","拉脱维亚 ",428,0,0,1,1};
medal_list[28] = {"Poland \t","波兰 \t",616,0,0,1,1};
}
4、排序函数
用sort函数实现对数据的排序,sort的参数格式如下
sort(first_pointer,first_pointer+n,cmp)
这里的cmp是自定义的排序方法
根据国际规则,奥运会奖牌榜的排序依次是将金牌、银牌、铜牌数量按降序排序,如果三者数据一样,则按照该国家或者地区的世界编码升序排序,则cmp的实现方法如下
bool cmp(const MedalList &a,const MedalList &b)
{
if(a.gold != b.gold) return a.gold>b.gold;
else if (a.silver != b.silver) return a.silver>b.silver;
else if (a.bronze != b.bronze) return a.bronze>b.bronze;
else if (a.world_code != b.world_code) return a.world_code<b.world_code;
}
5、输出函数
void show_medal_list()
{
cout<<"名次 \t"<<"国家 \t\t"<<"金牌 \t"<<"银牌 \t"<<"铜牌 \t"<<"总计 \t"<<endl;
int ranking=0,cnt=0;
for (int i = 0; i < N; i++)
{
ranking = i + 1;
if (medal_list[i].gold==medal_list[i-1].gold && \
medal_list[i].silver==medal_list[i-1].silver && \
medal_list[i].bronze==medal_list[i-1].bronze)
{
cnt += 1;
ranking -= cnt;
}
else
{
cnt = 0;
}
cout<<ranking<<"\t"<<medal_list[i].CountryName_cn<<"\t"<<medal_list[i].gold\
<<"\t"<<medal_list[i].silver<<"\t"<<medal_list[i].bronze<<"\t"\
<<medal_list[i].total<<endl;
}
}
这里的难点是若出现了两个或者以上的国家金银铜牌数据一样,则这些国家的位次一样,然后下一个位次的国家应该在这个位次的基础上加上同位次的国家数量,这里用了一个cnt变量来处理,当出现这种情况是,对cnt自增,然后位次ranking-cnt;否则,将cnt赋值为0
6、主函数
int main()
{
get_data(); //获取数据
random_shuffle(medal_list,medal_list+N); //打乱顺序
sort(medal_list,medal_list+N,cmp); //按照国际奥运奖牌榜排序方法排序
show_medal_list(); //展示奖牌榜
return 0;
}
7、完整代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 29;
struct MedalList
{
string CountryName,CountryName_cn;
int world_code;
int gold,silver,bronze,total;
};
bool cmp(const MedalList &a,const MedalList &b)
{
if(a.gold != b.gold) return a.gold>b.gold;
else if (a.silver != b.silver) return a.silver>b.silver;
else if (a.bronze != b.bronze) return a.bronze>b.bronze;
else if (a.world_code != b.world_code) return a.world_code<b.world_code;
}
MedalList medal_list[N]; //鍏ㄥ眬鍙橀噺
void get_data()
{
medal_list[0] = {"Norway \t","挪威 \t",578,16,8,13,37};
medal_list[1] = {"German \t","德国 \t",276,12,10,5,27};
medal_list[2] = {"China \t","中国 \t",156,9,4,2,15};
medal_list[3] = {"America ","美国 \t",840,8,10,7,25};
medal_list[4] = {"Sweden \t","瑞典 \t",752,8,5,5,18};
medal_list[5] = {"Netherlands","荷兰 \t",528,8,5,4,17};
medal_list[6] = {"Austia ","奥地利 \t",040,7,7,4,18};
medal_list[7] = {"Switzerland","瑞士 \t",756,7,2,5,14};
medal_list[8] = {"ROC \t","俄罗斯奥委会",643,6,12,14,32};
medal_list[9] = {"France ","法国 \t",250,5,7,2,14};
medal_list[10] = {"Canada ","加拿大 \t",124,4,8,14,26};
medal_list[11] = {"Japan \t","日本 \t",392,3,6,9,18};
medal_list[12] = {"Italy \t","意大利 \t",380,2,7,8,17};
medal_list[13] = {"Koria \t","韩国 \t",410,2,5,2,9};
medal_list[14] = {"Snownenia","斯洛文尼亚",705,2,3,2,7};
medal_list[15] = {"Finland ","芬兰 \t",246,2,2,4,8};
medal_list[16] = {"New Zealand","新西兰 \t",554,2,1,0,3};
medal_list[17] = {"Australia","澳大利亚 ",036,1,2,1,4};
medal_list[18] = {"UK \t","英国 \t",826,1,1,0,2};
medal_list[19] = {"Hungary ","匈牙利 \t",348,1,0,2,3};
medal_list[20] = {"Belgium ","比利时 \t",056,1,0,1,2};
medal_list[21] = {"Chech \t","捷克 \t",203,1,0,1,2};
medal_list[22] = {"Strova ","斯洛伐克 ",703,1,0,1,2};
medal_list[23] = {"Belarus ","白俄罗斯 ",112,0,2,0,2};
medal_list[24] = {"Spain \t","西班牙 \t",724,0,1,0,1};
medal_list[25] = {"Ukrain ","乌克兰 \t",804,0,1,0,1};
medal_list[26] = {"Esthonia ","爱沙尼亚 ",233,0,0,1,1};
medal_list[27] = {"Latvia \t","拉脱维亚 ",428,0,0,1,1};
medal_list[28] = {"Poland \t","波兰 \t",616,0,0,1,1};
}
void show_medal_list()
{
cout<<"名次 \t"<<"国家 \t\t"<<"金牌 \t"<<"银牌 \t"<<"铜牌 \t"<<"总计 \t"<<endl;
int ranking=0,cnt=0;
for (int i = 0; i < N; i++)
{
ranking = i + 1;
if (medal_list[i].gold==medal_list[i-1].gold && \
medal_list[i].silver==medal_list[i-1].silver && \
medal_list[i].bronze==medal_list[i-1].bronze)
{
cnt += 1;
ranking -= cnt;
}
else
{
cnt = 0;
}
cout<<ranking<<"\t"<<medal_list[i].CountryName_cn<<"\t"<<medal_list[i].gold\
<<"\t"<<medal_list[i].silver<<"\t"<<medal_list[i].bronze<<"\t"\
<<medal_list[i].total<<endl;
}
}
int main()
{
get_data(); //获取数据
random_shuffle(medal_list,medal_list+N); //打乱顺序
sort(medal_list,medal_list+N,cmp); //按照国际奥运奖牌榜排序方法排序
show_medal_list(); //展示奖牌榜
return 0;
}
8、运行结果
9、报错处理
中文在vscode中断乱码问题
这是因为vscode中默认的编码方式为utf-8,而终端采用的是gbk编码方式,因此我们需要将程序重新以gbk编码方式打开即可