首页 > 试题广场 >

成绩排序

[编程题]成绩排序
  • 热度指数:23171 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
有N个学生的数据,将学生数据按成绩从低到高排序,如果成绩相同则按姓名字符的字典序由小到大排序,如果姓名的字典序也相同则按照学生的年龄从小到大排序,并输出N个学生排序后的信息。

输入描述:
测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。
每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。


输出描述:
将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。
然后输出学生信息,按照如下格式:
姓名 年龄 成绩

学生姓名的字母序区分字母的大小写,如A要比a的字母序靠前(因为A的ASC码比a的ASC码要小)。
示例1

输入

3
abc 20 99
bcd 19 97
bed 20 97

输出

bcd 19 97
bed 20 97
abc 20 99
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct data
{
    string name;//直接用string类便于姓名的比较
    int age;
    int grades;
    bool operator<(const data&a)//重载<运算符
    {
        if(grades!=a.grades)return grades<a.grades;
        else if(name!=a.name)return name<a.name;
        else return age<a.age;
    }
}buf[1001];
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)cin>>buf[i].name>>buf[i].age>>buf[i].grades;
        sort(buf,buf+n);
        for(int i=0;i<n;i++)cout<<buf[i].name<<buf[i].age<<buf[i].grades<<endl;
    }
    return 0;
}

发表于 2020-02-15 17:04:05 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct student{
    char name[100];
    int age;
    int score;
};
int main(){
    int n;
    int i,j;
    struct student *s;
    scanf("%d",&n);
    s =(struct student *)malloc(sizeof(struct student)*n);
    for(i=0;i<n;i++){
        scanf("%s %d %d",&s[i].name,&s[i].age,&s[i].score);
    }
     for(i = n-1; i >= 0; i--)
    {
        for(j = 0; j < i; j++)
        {
            if((s[j].score > s[j+1].score) || (s[j].score == s[j+1].score && strcmp(s[j].name,s[j+1].name)>0)
               || (s[j].score == s[j+1].score && strcmp(s[j].name,s[j+1].name)==0 && s[j].age > s[j+1].age))
            {
                struct student tmp = s[j];
                s[j] = s[j+1];
                s[j+1] = tmp;
            }
        }
    }
    for(i=0; i < n-1; i++)
    {
        printf("%s %d %d\n",s[i].name, s[i].age, s[i].score);
    }
    i=n-1;
     printf("%s %d %d\n",s[i].name, s[i].age, s[i].score);
    return 0;
}
发表于 2018-06-02 18:26:52 回复(0)
我只想说,测试数据量大于100,如果不用动态分配请设置的大一点的数组,不然就是:段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)
发表于 2018-03-17 20:33:36 回复(0)
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
//自定义排序的思想
typedef struct Student
{
    char name[101];
    int age;
    int score;
};

bool operator<(const Student & s_1,const Student & s_2)
{
    int c = strcmp(s_1.name, s_2.name);//比较字符串大小,相等为0,s_1比s_2小为负数,否则为正
    if(s_1.score == s_2.score)
    {
        if(c == 0)
            return s_1.age < s_2.age;
        else
            return c < 0;
    }
    else
        return s_1.score < s_2.score;
}

int main(void)
{
    int n;
    Student student[1001];
    
    while(cin >> n)
    {
        for(int i = 0;i < n;i++)
        {
            cin >> student[i].name;
            int age;
            cin >> age;
            student[i].age = age;
            int score;
            cin >> score;
            student[i].score = score;
        }
        
        sort(student, student + n);
        
        for(int i = 0;i < n;i++)
        {
            cout << student[i].name << ' ' << student[i].age << ' ' << student[i].score;
            cout << endl;
        }
    }
    return 0;
}

发表于 2021-03-17 15:29:29 回复(0)
#include<stdio.h>//思想1.先按照分数排序2.如果分数相同在按照姓名3.再相同则按照年龄
#include<string.h>
struct Student{
    char name[10];int age;int score;
}stu[1000];
int main()
{
    int n,i,j;scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%s%d%d",stu[i].name,&stu[i].age,&stu[i].score);
    int ag,sc;char na[10];//1.先按成绩排序
    for(i=0;i<n-1;i++)
        for(j=0;j<n-1-i;j++)
            if(stu[j].score>stu[j+1].score)
            {//全部交换
                strcpy(na,stu[j].name);ag=stu[j].age;sc=stu[j].score;
                strcpy(stu[j].name,stu[j+1].name);stu[j].age=stu[j+1].age;stu[j].score=stu[j+1].score;
                strcpy(stu[j+1].name,na);stu[j+1].age=ag;stu[j+1].score=sc;
            }
    //2.成绩相同按照姓名排序3.姓名还是相同按年龄排序
    for(i=0;i<n-1;i++)//注意这里也用排序, 只不过排序的时候加了条件只有成绩相同的时候在判断是否要从新排序
        for(j=0;j<n-1-i;j++)
            if(stu[j].score==stu[j+1].score)//成绩相同
            {
                if(strcmp(stu[j].name,stu[j+1].name)>0)
                {//交换
                    strcpy(na,stu[j].name);ag=stu[j].age;sc=stu[j].score;
                    strcpy(stu[j].name,stu[j+1].name);stu[j].age=stu[j+1].age;stu[j].score=stu[j+1].score;
                    strcpy(stu[j+1].name,na);stu[j+1].age=ag;stu[j+1].score=sc;
                }
                if(strcmp(stu[j].name,stu[j+1].name)==0)//姓名相同
                {
                    if(stu[j].age>stu[j+1].age)
                    {//交换
                        strcpy(na,stu[j].name);ag=stu[j].age;sc=stu[j].score;
                        strcpy(stu[j].name,stu[j+1].name);stu[j].age=stu[j+1].age;stu[j].score=stu[j+1].score;
                        strcpy(stu[j+1].name,na);stu[j+1].age=ag;stu[j+1].score=sc;
                    }
                }
            }
    for(i=0;i<n;i++)//输出
        printf("%s %d %d\n",stu[i].name,stu[i].age,stu[i].score);
}
编辑于 2020-03-30 13:09:43 回复(0)
Java 面向对象写法,清晰可扩展
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            Student[] arr = new Student[n];
            for (int i = 0; i < n; i++) arr[i] = new Student(scanner.next(),scanner.nextInt(),scanner.nextInt());
            Arrays.sort(arr);
            for (Student student : arr) System.out.println(student);
        }
    }

    static class Student implements Comparable<Student>{
        String name;
        Integer age;
        Integer score;

        public Student(String name, Integer age, Integer score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }

        //将学生数据按成绩从低到高排序,如果成绩相同则按姓名字符的字典序由小到大排序,如果姓名的字典序也相同则按照学生的年龄从小到大排序
        @Override
        public int compareTo(Student o) {
            if (this.score.compareTo(o.score)==0){
                if (this.name.compareTo(o.name)==0) return this.age.compareTo(o.age);
                else return this.name.compareTo(o.name);
            }else return this.score.compareTo(o.score);
        }

        //姓名 年龄 成绩
        @Override
        public String toString() {
            return name+" "+age+" "+score ;
        }
    }
}


发表于 2020-03-18 16:58:36 回复(0)
调用sort函数,写好cmp即可
#include<bits/stdc++.h>
using namespace std;
struct student{
    char name[100];
    int age;
    int score;
};
student s[1000];
bool cmp(student x,student y){
    if(x.score!=y.score)
        return x.score<y.score;
    else if(x.score==y.score&&strcmp(x.name,y.name)!=0)
        return strcmp(x.name,y.name)<0;
    else 
        return x.age<y.age;
}
int main(){
    int N;
    while(cin>>N){
        for(int i=0;i<N;i++){
            cin>>s[i].name>>s[i].age>>s[i].score;
        }
        sort(s,s+N,cmp);
        for(int i=0;i<N;i++){
            cout<<s[i].name<<" "<<s[i].age<<" "<<s[i].score<<endl;
        }
    }
    return 0;
}

发表于 2020-02-23 11:28:36 回复(0)

Comparator接口的int compare(o1,o2)方法:compare()方法

返回负数表明o1(所代表的属性值)<o2(所代表的属性值),返回正数表明o1的大于o2的,如果返回0表明o1、o2相等

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
class Student {
    private String name;
    private int age;
    private int grade;
    public Student(String name, int age, int grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public int getGrade() {
        return grade;
    }
    public String toString() {
        return name + " " + age + " " + grade;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            int n = scan.nextInt();
            List list = new ArrayList(n);
            for (int i = 0; i < n; i++) {
                String name = scan.next();
                int age = scan.nextInt();
                int grade = scan.nextInt();
                list.add(new Student(name, age, grade));
            }
            Collections.sort(list, new MyComparator());
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).toString());
            }
        }
    }
    private static class MyComparator implements Comparator {
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.getGrade() != o2.getGrade()) {
                return o1.getGrade() > o2.getGrade() ? 1 : -1;
            } else if (o1.getName().compareTo(o2.getName()) != 0) {
                if (o1.getName().compareTo(o2.getName()) > 0) {
                    return 1;
                } else {
                    return -1;
                }
            } else {
                return o1.getAge() - o2.getAge();//默认升序
            }
        }
    }
}
编辑于 2018-05-29 10:38:23 回复(0)

#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

struct stu {

    string name;

    int age;

    int score;

    bool operator <(const stu &A) {

        if (score < A.score) {

            return 1;

        }

        else if (score == A.score) {

            if (name < A.name) {

                return 1;

            }

            else if (name == A.name) {

                if (age < A.age) {

                    return 1;

                }

            }

        }

        return 0;

    }

}s[1001];

int main() {

    int n;

    while (cin >> n) {

        for (int i = 0; i < n; i++) {

            cin >> s[i].name;

            cin >> s[i].age;

            cin >> s[i].score;

        }

        sort(s,s+n);

        for (int i = 0; i < n; i++) {

            cout << s[i].name << " " << s[i].age << " " << s[i].score << endl;

        }

    }

    return 0;

}


发表于 2018-03-06 16:15:05 回复(0)
#include<string.h>
struct Stu{char name[101];int age,grade;}stus[1000];
int cmp(const void* a,const void* b){
    struct Stu* s1 = (struct Stu*)a;struct Stu* s2 = (struct Stu*)b;
    return s1->grade != s2->grade?s1->grade - s2->grade:(strcmp(s1->name,s2->name)!=0?strcmp(s1->name,s2->name):s1->age - s2->age);
}
main(){
    int n,i;
    for(;~scanf("%d",&n);){
        for (i = 0; i < n &&~scanf("%s %d %d",stus[i].name,&stus[i].age,&stus[i].grade); i++);
        qsort(stus,n,sizeof(struct Stu),cmp);
        for (i = 0; i < n && printf("%s %d %d\n",stus[i].name,stus[i].age,stus[i].grade); i++);
    }
}
编辑于 2018-01-10 21:07:21 回复(0)
#include<stdio.h>
#include<string.h>
int main()
{
    typedef struct student
    {
        char name[100];
        int age,score;
    }student;
    student student1[1000],student2;
    int N,n,j;
    while(scanf("%d",&N)!=EOF)
    {
        for(n=0;n<N;n++)
            scanf("%s%d%d",student1[n].name,&student1[n].age,&student1[n].score);
        for(n=0;n<N;n++)
            for(j=0;j<N-n-1;j++)
            if(student1[j].score>student1[j+1].score)
        {
            student2=student1[j];
            student1[j]=student1[j+1];
            student1[j+1]=student2;
        }
        else if(student1[j].score==student1[j+1].score)
             if(strcmp(student1[j].name,student1[j+1].name)>0)
               {
            student2=student1[j];
            student1[j]=student1[j+1];
            student1[j+1]=student2;
        }
            else if(strcmp(student1[j].name,student1[j+1].name)==0)
                 if(student1[j].age>student1[j+1].age)
                     {
            student2=student1[j];
            student1[j]=student1[j+1];
            student1[j+1]=student2;
        }
        for(n=0;n<N;n++)
            printf("%s %d %d\n",student1[n].name,student1[n].age,student1[n].score);

    }
    return 0;
}
发表于 2017-12-31 13:01:24 回复(0)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

typedef struct
{
	string name;
	int age, socre;
}student;

bool cmpsocre(student x, student y)
{
	if (x.socre == y.socre && x.name == y.name)
		return x.age < y.age;
	else if (x.socre == y.socre && x.name != y.name)
		return x.name < y.name;
	else
		return x.socre < y.socre;
}
int main()
{
	int length;
	while (cin >> length)
	{
		vector<student> data;
		student s;
		for (int i = 0; i < length; i++)
		{
			cin >> s.name >> s.age >> s.socre;
			data.push_back(s);
		}
		sort(data.begin(), data.end(), cmpsocre);
		for (int i = 0; i < length; i++)
			cout << data[i].name << " " << data[i].age << " " << data[i].socre << endl;		
	}
	return 0;
}

发表于 2017-05-01 15:13:00 回复(1)
 
编辑于 2019-01-11 23:26:07 回复(0)
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct student{
    char name[110];
    int age;
    int grade;
};
bool cmp(student a,student b){
    if(a.grade==b.grade){
        if(strcmp(a.name,b.name)==0)return a.age<b.age;
        else return strcmp(a.name,b.name)<0;
    }
    else return a.grade<b.grade;
}
int main(){
    int N,n;
    student stu[1001];
    while(scanf("%d",&N)!=EOF){
        for(int i=0;i<N;i++){
            scanf("%s %d %d",stu[i].name,&stu[i].age,&stu[i].grade);
        }
        sort(stu,stu+N,cmp);
        for(n=0;n<N-1;n++){
            printf("%s %d %d\n",stu[n].name,stu[n].age,stu[n].grade);
        }
        printf("%s %d %d",stu[n].name,stu[n].age,stu[n].grade);
    }
    return 0;
}
 
C+stl
发表于 2018-02-21 20:54:00 回复(5)
使用strcmp(const char *s1,const char * s2),这里面只能比较char类型字符串,不能使用string类型,头文件需引入<string.h>,后面加.h 
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;

struct student{
    char name[100];
    int age;
    int score;
};

bool cmp(const student s1,const student s2){
    if(s1.score!=s2.score) return s1.score<s2.score;
    else if(strcmp(s1.name,s2.name)!=0) return strcmp(s1.name,s2.name)<0;
    else return s1.age<s2.age;    
}

int main(){
    int n;
    while(cin>>n){
        int i;
        vector<student> students(n);
        for(i=0;i<n;i++)
            cin>>students[i].name>>students[i].age>>students[i].score;
        sort(students.begin(),students.end(),cmp);
        for(i=0;i<n;i++)
            cout<<students[i].name<<" "<<students[i].age<<" "<<students[i].score<<endl;
    }
    return 0;
}

编辑于 2017-12-04 21:20:00 回复(2)

Object C

#include<stdio.h>
#include<stdlib.h>

typedef struct Student{
    char name[110];
    int age, grade;
}Student;
Student stu[1010];
int cmp(const void *a, const void *b){
    struct Student *s1 = (Student *) a;
    struct Student *s2 = (Student *) b;
    if(s1->grade != s2->grade) return s1->grade-s2->grade;
    if(strcmp(s1->name, s2->name)) return strcmp(s1->name, s2->name);
    return s1->age-s2->age;
}
int main(){
    //freopen("aa", "r", stdin);
    int n, i;
    while(~scanf("%d", &n)){
        for(i = 0; i<n; ++i){
            scanf("%s %d %d", stu[i].name, &stu[i].age, &stu[i].grade);

        }
        qsort(stu, n, sizeof(stu[0]), cmp);
        for(i = 0; i<n; i++)
            printf("%s %d %d\n", stu[i].name, stu[i].age, stu[i].grade);


    }

    return 0;
}
发表于 2018-07-12 17:03:24 回复(1)
三趟冒泡排序;
第一趟:对年龄排序
第二趟:对名字排序
第三趟:对成绩排序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1000
typedef struct student{
    int grade;
    char name[20];
    int year;
}student;
int main(){
    student s[N];
    int n,i,j,k=0;
    student temp;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s",s[i].name);
        scanf("%d",&s[i].year);
        scanf("%d",&s[i].grade);
    }
    for(i=0;i<n-1;i++){
        for(j=0;j<n-i-1;j++){
            if(s[j].year>s[j+1].year){
                temp=s[j];
                s[j]=s[j+1];
                s[j+1]=temp;
            }
        }
    }
    for(i=0;i<n-1;i++){
        for(j=0;j<n-i-1;j++){
            while((s[j].name[k]==s[j+1].name[k])&&(s[j].name[k]!='\0')){
                k++;
            }
            if(s[j].name[k]>s[j+1].name[k]){
                temp=s[j];
                s[j]=s[j+1];
                s[j+1]=temp;
            }
            k=0;
        }
    }
    for(i=0;i<n-1;i++){
        for(j=0;j<n-i-1;j++){
            if(s[j].grade>s[j+1].grade){
                temp=s[j];
                s[j]=s[j+1];
                s[j+1]=temp;
            }
        }
    }
    for(i=0;i<n;i++){
        printf("%s %d %d",s[i].name,s[i].year,s[i].grade);
        printf("\n");
    }
    return 0;
}
发表于 2020-01-04 23:16:28 回复(0)
#include<iostream>
#
include<algorithm>
using namespace std;
struct stu{
    string name;
    int age,score;
};
bool cmp(stu s1,stu s2){
    if(s1.score!=s2.score) return s1.score<s2.score;
    else if(s1.name!=s2.name) return s1.name<s2.name;
    else return s1.age<s2.age;
}
int main(){
    int n;
    while(cin>>n){
        stu st[n];
        for(int i=0;i<n;++i){
            cin>>st[i].name>>st[i].age>>st[i].score;
        }
        sort(st,st+n,cmp);
        for(int i=0;i<n;++i){
            cout<<st[i].name<<" "<<st[i].age<<" "<<st[i].score<<endl;
        }
    }
    return 0;
}
发表于 2020-03-06 14:50:55 回复(0)

真男人就要手写快排算法。

#include <bits/stdc++.h>
using namespace std;

struct Student
{
    int score;
    string name;
    int age;
    bool operator == (const Student &s) const
    {
        return score == s.score && name == s.name && age == s.age;
    }
    bool operator < (const Student &s) const
    {
        if (this->score != s.score)
            return this->score < s.score;
        else if (this->name != s.name)
            return this->name < s.name;
        else
            return this->age < s.age;
    }
    bool operator <= (const Student &s) const
    {
        return *this < s || *this == s;
    }
};

Student sarr[1000];

int Partition(int first, int last)
{
    Student pVal = sarr[first];
    while (first < last)
    {
        while (first < last && pVal <= sarr[last])
            --last;
        sarr[first] = sarr[last];
        while (first < last && sarr[first] <= pVal)
            ++first;
        sarr[last] = sarr[first];
    }
    sarr[first] = pVal;
    return first;
}

void QuickSort(int beg, int end)
{
    if (beg + 1 < end)
    {
        int pivot = Partition(beg, end - 1);
        QuickSort(beg, pivot);
        QuickSort(pivot + 1, end);
    }
}

int main()
{
    int n;
    while (cin >> n)
    {
        for (int i = 0; i < n; ++i)
            cin >> sarr[i].name >> sarr[i].age >> sarr[i].score;
        QuickSort(0, n);
        for (int i = 0; i < n; ++i)
            cout << sarr[i].name << " " << sarr[i].age << " " << sarr[i].score << endl;
    }
    return 0;
}
发表于 2019-07-21 13:14:10 回复(0)
import java.util.*;


public class Main{
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int len=scanner.nextInt();
        scanner.nextLine();
        Student [] students=new Student[len];
        for (int i=0;i<len;i++){
            Student temp=new Student();
            String str=scanner.next();
            int age=scanner.nextInt();
            int score=scanner.nextInt();
            temp.setName(str);
            temp.setAge(age);
            temp.setScore(score);
            students[i]=temp;
        }
        Student_sort sort=new Student_sort(students);
        sort.bubbleSort();
        sort.print();
    }
}


class Student_sort{

    private Student [] students;

    public Student_sort(Student [] students) {
        this.students=students;
    }

    public void bubbleSort(){
        for (int i=1;i<students.length;i++){
            for (int j=0;j<students.length-i;j++){
                if (students[j].getScore()>students[j+1].getScore()){//比较分数
                    Student temp=students[j];
                    students[j]=students[j+1];
                    students[j+1]=temp;
                }else if (students[j].getScore()==students[j+1].getScore()){//如果分数相同
                    if (students[j].getName().length()>students[j+1].getName().length()){//比较字符串长度
                        Student temp=students[j];
                        students[j]=students[j+1];
                        students[j+1]=temp;
                    }else if (students[j].getName().length()==students[j+1].getName().length()){//如果字符串长度相等
                        int count1=0;
                        int count2=0;
                        for (int m=0;m<students[j].getName().length();m++){
                            count1+=students[j].getName().charAt(m)-'0';
                            count2+=students[j+1].getName().charAt(m)-'0';
                        }
                        if (count1>count2){
                            Student temp=students[j];
                            students[j]=students[j+1];
                            students[j+1]=temp;
                        }else if (count1==count2){
                            if (students[j].getAge()>students[j+1].getAge()){//比较年龄
                                Student temp=students[j];
                                students[j]=students[j+1];
                                students[j+1]=temp;
                            }
                        }
                    }
                }
            }
        }
    }
    public void print(){
        for (int i=0;i<students.length;i++){
            System.out.println(students[i].getName()+" "+students[i].getAge()+" "+students[i].getScore());
        }
    }
}
class Student{
    private String name;
    private int age;
    private int score;

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}
发表于 2017-03-16 09:39:12 回复(0)