C++里怎么编写成绩排序

那位先生_ 2012-03-15 11:59:04
每个学号对应一个成绩,对成绩进行排序,输出时学号与成绩的对应关系和原来的不能变。
...全文
760 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigbigboy2012 2012-03-18
  • 打赏
  • 举报
回复
用multimap,如果VC++6.0平台,要#pragma warning(disable: 4786)
Furney 2012-03-18
  • 打赏
  • 举报
回复
来晚啦,上面的解答已经很完整啦
qq120848369 2012-03-16
  • 打赏
  • 举报
回复
建议使用student* *ptr_arr;存储数据,无论是扩容realloc还是排序sort都会很高效。
nfme 2012-03-16
  • 打赏
  • 举报
回复
用结构体,类,map都行。
网安小明 2012-03-16
  • 打赏
  • 举报
回复
以前做过,似乎是作业吧?这样不好。

可以定义结构体数组。
hongwenjun 2012-03-16
  • 打赏
  • 举报
回复
#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;
}



蒋培成 => 68
卢小珍 => 82
童万贞 => 71
童云龙 => 88
谢建国 => 78
姚伟林 => 98
______________________________ 按成绩降序排序 ______________________________
姚伟林 => 98
童云龙 => 88
卢小珍 => 82
谢建国 => 78
童万贞 => 71
蒋培成 => 68

Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
诶呦 2012-03-16
  • 打赏
  • 举报
回复
map吧
  • 打赏
  • 举报
回复

#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;
}



使用结构体定义学生的结构,使用alogrithm库里的sort函数进行排序,自定义cmp的排序方法对结构体排序。
dic_008 2012-03-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 pathuang68 的回复:]

用STL中的map和algorithm很适合解决楼主这样的问题。
[/Quote]

++
pathuang68 2012-03-16
  • 打赏
  • 举报
回复
用STL中的map和algorithm很适合解决楼主这样的问题。
pathuang68 2012-03-16
  • 打赏
  • 举报
回复
用STL中的map和algorithm好了

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧