首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++里如何编写成绩排序

2012-04-12 
C++里怎么编写成绩排序每个学号对应一个成绩,对成绩进行排序,输出时学号与成绩的对应关系和原来的不能变。[

C++里怎么编写成绩排序
每个学号对应一个成绩,对成绩进行排序,输出时学号与成绩的对应关系和原来的不能变。

[解决办法]
以前做过,似乎是作业吧?这样不好。

可以定义结构体数组。

[解决办法]
用结构体,类,map都行。
[解决办法]
用STL中的map和algorithm好了
[解决办法]

探讨

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

[解决办法]
C/C++ code
#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;}
[解决办法]
C/C++ code
#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;} 

热点排行