64,685
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std;
struct stu {
char name[16];
int score;
};
int cmp(stu a, stu b)
{
return a.score > b.score;
}
int main()
{
map<string , int> ScoreMap = {
{"卢小珍", 82}, {"童云龙", 88}, {"童万贞", 71},
{"蒋培成", 68}, {"姚伟林", 98}, {"谢建国", 78}
};
list<stu> ScoreList;
stu tmp;
for (auto it = ScoreMap.begin(); it != ScoreMap.end() ; ++it) {
cout << it->first << " => " << it->second << endl;
strcpy(tmp.name, it->first.c_str());
tmp.score = it->second;
ScoreList.push_back(tmp);
}
cout << string(30, '_') << " 按成绩降序排序 " << string(30, '_') << endl;
ScoreList.sort(cmp);
for (auto it = ScoreList.begin(); it != ScoreList.end() ; ++it) {
cout << it->name << " => " << it->score << endl;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct st_Student
{
int ID;
int grade;
};
int cmp( st_Student a, st_Student b )
{
return a.grade > b.grade;
}
int main ( void )
{
st_Student Stu[50];
int n;
do
{
cout << "请输入学生人数(0退出)" << endl;
cin >> n;
if( n == 0 ) break;
for( int i = 0 ; i < n ; i++ )
{
Stu[i].ID = i;
cout << "请输入" << i << "号学生的成绩" << endl;
cin >> Stu[i].grade;
}
sort( Stu, Stu+n, cmp );
cout << "依据成绩排序完毕" << endl;
for( int i = 0 ; i < n ; i++ )
{
cout << "当前学生编号为" << Stu[i].ID << "\t成绩为" << Stu[i].grade << endl;
}
cout << endl;
}while(1);
return 0;
}