zoukankan      html  css  js  c++  java
  • 题目1061:成绩排序

    题目链接:http://ac.jobdu.com/problem.php?pid=1061

    分析见:https://github.com/zpfbuaa/JobduInCPlusPlus

    参考代码1:

    //
    //  1061 成绩排序.cpp
    //  oj
    //
    //  Created by PengFei_Zheng on 03/04/2017.
    //  Copyright © 2017 PengFei_Zheng. All rights reserved.
    //
     
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <string.h>
    #include <cstring>
     
    using namespace std;
     
    int n;
    struct Stu{
        string name;
        int age;
        int grade;
    }stu[1001];
     
    bool cmp(Stu a, Stu b){
        if(a.grade!=b.grade) return a.grade<b.grade;
        int result = strcmp(a.name.c_str(),b.name.c_str());
        if(result) return result<0;
        else return a.age<b.age;
    }
    int main(){
        while(cin>>n){
            for(int i = 0 ; i < n ; i++){
                cin>>stu[i].name>>stu[i].age>>stu[i].grade;
            }
            sort(stu,stu+n,cmp);
            for(int i =0 ; i < n ; i ++){
                cout<<stu[i].name<<" "<<stu[i].age<<" "<<stu[i].grade<<endl;
            }
        }
        return 0;
         
    }
     
    /**************************************************************
        Problem: 1061
        User: zpfbuaa
        Language: C++
        Result: Accepted
        Time:620 ms
        Memory:1544 kb
    ****************************************************************/

    参考代码2:

    //
    //  1061 成绩排序.cpp
    //  oj
    //
    //  Created by PengFei_Zheng on 03/04/2017.
    //  Copyright © 2017 PengFei_Zheng. All rights reserved.
    //
     
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <string.h>
    #include <cstring>
     
    using namespace std;
     
    int n;
    struct Stu{
        string name;
        int age;
        int grade;
        bool operator < (const Stu &b) const{
            if(grade!=b.grade) return grade<b.grade;
            int temp = strcmp(name.c_str(), b.name.c_str());
            if(temp!=0)return temp<0;
            else return age<b.age;
        }
    }stu[1001];
     
    int main(){
        while(cin>>n){
            for(int i = 0 ; i < n ; i++){
                cin>>stu[i].name>>stu[i].age>>stu[i].grade;
            }
            sort(stu,stu+n);
            for(int i =0 ; i < n ; i ++){
                cout<<stu[i].name<<" "<<stu[i].age<<" "<<stu[i].grade<<endl;
            }
        }
        return 0;
         
    }
     
    /**************************************************************
        Problem: 1061
        User: zpfbuaa
        Language: C++
        Result: Accepted
        Time:570 ms
        Memory:1540 kb
    ****************************************************************/
  • 相关阅读:
    合并二叉树
    剑指 Offer 68
    剑指 Offer 42. 连续子数组的最大和
    C语言 递归实现迷宫寻路问题
    C语言数据结构 统计英文文字每个“单词”出现次数
    C语言 双链表的频度排序管理
    C语言 插入排序使链表递增
    c语言 简单的天数计算器
    数据结构 链表的头插法逆置
    C语言 删除指定的单词
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/6671377.html
Copyright © 2011-2022 走看看