成绩排序

题目描述

查找和排序 :输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩都按先录入排列在前的规则处理。


输入例子:
3
0
fang 90
yang 50
ning 70
输出例子:
fang 90
ning 70
yang 50

【代码】

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;

  6. struct student
  7. {
  8.    string name;
  9.    int score;
  10. };
  11.  
  12. bool cmp0(const student &a, const student &b)  //高到低
  13. {
  14.     return a.score > b.score;
  15. }
  16.   
  17. bool cmp1(const student &a, const student &b)   //低到高
  18. {
  19.    return a.score < b.score;
  20. }
  21.  

  22. int main()
  23. {
  24.     int n, cmp;
  25.     while(cin>>n>>cmp)
  26.     {  
  27.        vector<student> stu(n);
  28.         
  29.        for(int i=0; i<n; i++)
  30.            cin>> stu[i].name >> stu[i].score;  
  31.         
  32.       if(cmp==0)
  33.           stable_sort(stu.begin(), stu.end(), cmp0);
  34.       else
  35.           stable_sort(stu.begin(), stu.end(), cmp1);
  36.   
  37.        for(int i=0; i<n; i++)
  38.            cout<< stu[i].name <<" "<<stu[i].score<<endl;
  39.    
  40.       }
  41.  return 0;
  42. }
  43.      


******************************************       相关   *****************************************************

题目描述

用一维数组存储学号和成绩,然后,按成绩排序输出。 
输入描述:
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
输出描述:
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。
输入例子:
3
1 90
2 87
3 92
输出例子:
2 87
1 90
3 92

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;

  6. struct student
  7. {
  8.    int number;
  9.    int score;
  10. };
  11.  
  12. bool comp(const student &a, const student &b)   
  13. {
  14.    if(a.score != b.score)
  15.                          return a.score < b.score;
  16.     else
  17.                  return a.number < b.number;
  18. }
  19.  

  20. int main()
  21. {
  22.    student stu;
  23.    vector<student> v;
  24.    
  25.    int n;
  26.    while(cin>>n)
  27.    {
  28.       for(int i=0; i<n; i++)
  29.       {
  30.          cin>>stu.number>>stu.score;
  31.          v.push_back(stu);
  32.        }
  33.  
  34.       sort(v.begin(), v.end(), comp);
  35.  
  36.       vector<student>::iterator it;
  37.       for(it=v.begin(); it<v.end(); it++)
  38.       {
  39.          cout<<(*it).number<<" "<<(*it).score<<endl;
  40.        }
  41.      }
  42. return 0;
  43. }







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屠变恶龙之人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值