首页 > 试题广场 >

EXCEL排序

[编程题]EXCEL排序
  • 热度指数:8093 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。     对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

输入描述:
    测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。


输出描述:
    对每个测试用例,首先输出1行“Case:”。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
示例1

输入

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

输出

Case:
000001 Zoe 60
000007 James 85
000010 Amy 90
此类排序题有通用的做法。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
    char id[7];
    char name[9];
    int mark;
}stu;
int cmp1(stu *a,stu *b)
{
    return strcmp(a->id,b->id);
}
int cmp2(stu *a,stu *b)
{
    if(strcmp(a->name,b->name)==0)return strcmp(a->id,b->id);
    else return strcmp(a->name,b->name);
}
int cmp3(stu *a,stu *b)
{
    if((a->mark-b->mark)==0)return strcmp(a->id,b->id);
    else return a->mark-b->mark;
}
int main()
{
    int N,type,i;
    while(~scanf("%d%d",&N,&type) && N!=0)
    {
        stu s[N];
        for(i=0;i<N;i++)scanf("%s%s%d",s[i].id,s[i].name,&s[i].mark);
        if(type==1)qsort(s,N,sizeof(stu),cmp1);
        else if(type==2)qsort(s,N,sizeof(stu),cmp2);
        else qsort(s,N,sizeof(stu),cmp3);
        printf("Case:\n");
        for(i=0;i<N;i++)printf("%s %s %d\n",s[i].id,s[i].name,s[i].mark);
    }
}


发表于 2020-02-14 16:07:16 回复(0)
#include <iostream>
#include <algorithm>
using namespace std;
struct Student{	string id, name; int score;};
int n = 0, c = 0; Student sts[100000];
int MySort(Student sts1, Student sts2)
{
	switch (c)
	{
	case 1: return sts1.id < sts2.id;
	case 2: return sts1.name == sts2.name ? sts1.id < sts2.id : sts1.name < sts2.name;
	case 3: return sts1.score == sts2.score ? sts1.id < sts2.id : sts1.score < sts2.score;
	}
}
int main()
{
	while (cin >> n >> c && 0 != n) for (int i = 0; i < n; ++i) cin >> sts[i].id >> sts[i].name >> sts[i].score;
	sort(sts, sts + n, MySort); cout << "Case:" << endl; for (int i = 0; i < n; ++i) cout << sts[i].id << ' ' << sts[i].name << ' ' << sts[i].score << endl; return 0;
}

编辑于 2020-02-21 21:54:00 回复(0)
为甚会出现这种情况呢?请大佬为小白解答一二!!!
我的输出结果:
Case:
000001Zoe Zoe 60
000007James James 85
000010Amy Amy 90

附代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef struct Stu{
    char num[6];
    char name[8];
    int score;
}Stu;
bool cmp1(Stu &a,Stu &b){
    int tmp = strcmp(a.num,b.num);
    return tmp<0;
}
bool cmp2(Stu &a,Stu &b){
    int tmp = strcmp(a.name,b.name);
    return tmp<0;
}
bool cmp3(Stu &a,Stu &b){
    return a.score<b.score;
}
int main(){
    int N,C;
    scanf("%d%d",&N,&C);
    Stu * student = new Stu[N];
    for(int i=0;i<N;i++){
        scanf("%s%s%d",&student[i].num, &student[i].name,&student[i].score);
    }
     printf("Case:\n");
    switch(C){
        case 1:sort(student,student+N,cmp1);break;
        case 2:sort(student,student+N,cmp2);break;
        case 3:sort(student,student+N,cmp3);break;
    }
    for(int i=0;i<N;i++){
        printf("%s %s %d\n",student[i].num,student[i].name,student[i].score);
    }
    return 0;
}

感激大佬解答

发表于 2018-12-24 16:01:20 回复(6)

python solution

这是一个多key排序的问题,python解决这种问题跟切菜一样简单。

使用arr来保存输入的数据。

arr = []
a = input()
cnt = 1
while a != "0 0":

    num, key = map(int, a.split())
    for i in range(num):
        arr.append(input().split())

    print("Case " + str(cnt) + ":")
    if key == 1:
        arr.sort(key=lambda c: int(c[0]))
    elif key == 2:
        arr.sort(key=lambda c: (c[1], int(c[0])))
    else:
        arr.sort(key=lambda c: (int(c[2]), int(c[0])))
    for i in arr: print(" ".join(i))
    cnt += 1
    arr = []
    a = input()
发表于 2017-10-25 15:23:23 回复(2)
#include<bits/stdc++.h>
using namespace std;

struct student {
    string num;
    string name;
    int chengji;
} stu[100];

bool cmp1(student a, student b) {
    return a.num < b.num;
}

bool cmp2(student a, student b) {
    return a.name < b.name;
}

bool cmp3(student a, student b) {
    if (a.name == b.name || a.chengji == b.chengji) {
        return a.num < b.num;
    } else {
        return a.chengji < b.chengji;
    }
}

int main() {
    int n, c;
    while (cin >> n >> c) {
        if (n == 0) {
            break;
        }
        for (int i = 0; i < n; i++) {
            cin >> stu[i].num >> stu[i].name >> stu[i].chengji;
        }
        if (c == 1) {
            sort(stu, stu + n, cmp1);
        } else if (c == 2) {
            sort(stu, stu + n, cmp2);
        } else if (c == 3) {
            sort(stu, stu + n, cmp3);
        }
        cout << "Case:" << endl;
        for (int i = 0; i < n; i++) {
            cout << stu[i].num << " " << stu[i].name << " " << stu[i].chengji << endl;
        }
        cout << endl;
    }
    return 0;
}

发表于 2023-03-07 09:57:14 回复(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();
            int caseNumber = scanner.nextInt();
            Student[] students = new Student[n];
            for (int i = 0; i < n; i++) {
                String ids = scanner.next();
                int id = Integer.parseInt(ids);
                students[i] = new Student(ids, id, scanner.next(), scanner.nextInt(), caseNumber);
            }
            Arrays.sort(students);
            System.out.println("Case:");
            for (Student student : students) System.out.println(student);
        }
    }

    static class Student implements Comparable<Student> {
        String ids;
        Integer id;
        String name;
        Integer score;
        Integer caseNumber;

        public Student(String ids, Integer id, String name, Integer score, Integer caseNumber) {
            this.ids = ids;
            this.id = id;
            this.name = name;
            this.score = score;
            this.caseNumber = caseNumber;
        }

        @Override
        public int compareTo(Student o) {
            if (caseNumber == 1) return this.id.compareTo(o.id);
            else if (caseNumber == 2)
                return this.name.equals(o.name) ? this.id.compareTo(o.id) : this.name.compareTo(o.name);
                //caseNumber==3
            else return this.score.equals(o.score) ? this.id.compareTo(o.id) : this.score.compareTo(o.score);

        }

        @Override
        public String toString() {
            return ids + " " + name + " " + score;
        }
    }
}


发表于 2020-03-20 16:59:47 回复(0)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
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();
			int i = scanner.nextInt();
			List<Student> myList = new ArrayList<Student>();
			for (int j = 0; j < n; j++) {
				Student student = new Student(scanner.next(),scanner.next(),scanner.nextInt());
				myList.add(student);
			}
			
			if (i == 1) {
				Collections.sort(myList,Comparator.comparing(Student::getNo));
				System.out.println("Case:");
				for (Student student : myList) {
					System.out.println(student.no+" "+student.name+" "+student.grade);
				}
			}else if (i == 2) {
				Collections.sort(myList,Comparator.comparing(Student::getName));
				System.out.println("Case:");
				for (Student student : myList) {
					System.out.println(student.no+" "+student.name+" "+student.grade);
				}
			}else {
				Collections.sort(myList,Comparator.comparing(Student::getGrade).thenComparing(Student::getNo));
				System.out.println("Case:");
				for (Student student : myList) {
					System.out.println(student.no+" "+student.name+" "+student.grade);
				}
			}
			
		}
	}
}

class Student{

	String no;
	String name;
	int grade;
	
	public Student(String no,String name,int grade) {
		this.no = no;
		this.name = name;
		this.grade = grade;
	}
	
	public void setNo(String no) {
		this.no = no;
	}
	public String getNo() {
		return no;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public int getGrade() {
		return grade;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	
}

编辑于 2024-03-31 20:36:00 回复(0)
#include <bits/stdc++.h>
using namespace std;
int n,c;
struct arc{
	string num;
	string name;
	int grade;
};

bool cmp1(arc a,arc b)
{
	return stoi(a.num)<stoi(b.num);
}

bool cmp(arc a,arc b)
{
	if(c==2)	return a.name<=b.name;
	else if(c==3)	return a.grade<=b.grade;
}
int main(){
	while(cin>>n>>c)
	{
		if(n==0)	break;
		arc stu[n];
		for(int i=0;i<n;i++)
		{
			cin>>stu[i].num>>stu[i].name>>stu[i].grade;
		}
		sort(stu,stu+n,cmp1);
		if(c!=1)
		stable_sort(stu,stu+n,cmp);
		cout<<"case"<<':'<<endl;
		for(int i=0;i<n;i++)
		{
			cout<<stu[i].num<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
		}
	}
	return 0;
}

编辑于 2024-03-18 12:02:49 回复(0)
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int c;  //排序方式

struct Student {
    string id;      //学号
    string name;    //姓名
    int score;      //成绩
    bool operator<(const Student& stu) {
        switch (c) {
            case 1:
                return id < stu.id;
            case 2:
                return name < stu.name || (name == stu.name && id < stu.id);
            default:    //case 3:
                return score < stu.score || (score == stu.score && id < stu.id);
        }
    }
};

int main() {
    int n;
    while (cin >> n >> c) {
        vector<Student>arr(n);
        for (auto& stu : arr) {
            cin >> stu.id >> stu.name >> stu.score;
        }
        sort(arr.begin(), arr.end());
        cout << "Case:" << endl;
        for (const auto& stu : arr) {
            cout << stu.id << " " << stu.name << " " << stu.score << endl;
        }
    }
    return 0;
}

发表于 2024-03-03 14:40:14 回复(0)
//用C做的  思路比较清晰  不懂得同学可以看看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student{
    char num[10];
    char name[10];
    int score;
};
int cmp1(const void *a,const void *b){
    struct Student *m=(struct Student *)a;
    struct Student *n=(struct Student *)b;
    return strcmp(m->num, n->num);
}
int cmp2(const void *a,const void *b){
    struct Student *m=(struct Student *)a;
    struct Student *n=(struct Student *)b;
    if(m->name==n->name){
        return strcmp(m->num, n->num);
    }else{
        return strcmp(m->name,n->name);
    }
   
}
int cmp3(const void *a,const void *b){
    struct Student *m=(struct Student *)a;
    struct Student *n=(struct Student *)b;
    if(m->score==n->score){
        return strcmp(m->num,n->num);
    }else{
        return m->score-n->score;
    }
}

int main() {
    int number =0;
    int way=0;
    struct Student A[100000];
    while(scanf("%d %d",&number,&way)!=EOF){
        for(int i=0;i<number;i++){
        scanf("%s %s %d",&A[i].num,&A[i].name,&A[i].score);
    }
    if(way==1){
        qsort(A,number,sizeof(struct Student),cmp1);
    }else if(way==2){
        qsort(A,number,sizeof(struct Student),cmp2);
    }else if(way==3){
        qsort(A,number,sizeof(struct Student),cmp3);
    }
    printf("Case:\n");
    for(int i=0;i<number;i++){
        printf("%s %s %d\n",A[i].num,A[i].name,A[i].score);
    }
    }
   
}

发表于 2023-03-27 17:50:47 回复(0)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        inform a[]=new inform[n];
        for(int i=0;i<n;i++) {
            inform ne=new inform();
            ne.num=sc.next();
            ne.name=sc.next();
            ne.score=sc.nextInt();
            a[i]=ne;
        }
        if(k==1) {
            num_sort(a);//对学号进行排序
            System.out.println("Case:");
            for(int i=0;i<a.length;i++) {//输出排序后的内容
                System.out.print(a[i].num+" "+a[i].name+" "+a[i].score);
                System.out.println();
            }
        }
        else if(k==2) {
            name_sort(a);//对名字进行排序
            System.out.println("Case:");
            for(int i=0;i<a.length;i++) {//输出排序后的内容
                System.out.print(a[i].num+" "+a[i].name+" "+a[i].score);
                System.out.println();
            }
        }
        else if(k==3) {
            score_sort(a);//对成绩进行排序
            System.out.println("Case:");
            for(int i=0;i<a.length;i++) {//输出排序后的内容
                System.out.print(a[i].num+" "+a[i].name+" "+a[i].score);
                System.out.println();
            }
        }
        

    }
    public static void num_sort(inform []a) {
        for(int i=0;i<a.length-1;i++) {
            for(int j=i+1;j<a.length;j++) {
                if(a[i].num.compareTo(a[j].num)>0) {
                    inform temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
            }
        }
    }
    public static void name_sort(inform []a) {
        for(int i=0;i<a.length-1;i++) {
            for(int j=i+1;j<a.length;j++) {
                if(a[i].name.compareTo(a[j].name)>0) {
                    inform temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
                else if(a[i].name.compareTo(a[j].name)==0) {
                    if(a[i].num.compareTo(a[j].num)>0) {
                        inform temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
                }
            }
        }
    }
    public static void score_sort(inform []a) {
        for(int i=0;i<a.length-1;i++) {
            for(int j=i+1;j<a.length;j++) {
                if(a[i].score>a[j].score) {
                    inform temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
                else if(a[i].score==a[j].score) {
                    if(a[i].num.compareTo(a[j].num)>0) {
                        inform temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
                }
            }
        }
    }
    

}
class inform{
    String num;
    String name;
    int score;
}

发表于 2023-03-24 21:37:40 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct Student{
    string sno, name;
    int g;
};

int main() {
    int n, c;
    while (cin >> n >> c){
        vector<Student> v(n);
        for (auto &s : v)
            cin >> s.sno >> s.name >> s.g;
        sort(v.begin(), v.end(), [c](Student s1, Student s2)->bool{
            if (c == 1) return s1.sno.compare(s2.sno) < 0;
            if (c == 2) {
                if (s1.name.compare(s2.name) == 0) 
                    return s1.sno.compare(s2.sno) < 0;
                return s1.name.compare(s2.name) < 0;    
            }
            if (s1.g == s2.g) return s1.sno.compare(s2.sno) < 0;
            return s1.g < s2.g;
        });
        cout << "Case:" << endl;
        for (auto &s : v)
            cout << s.sno << " " << s.name << " " << s.g << endl;
    }
}
发表于 2023-03-17 21:27:47 回复(0)
试试函数指针数组,但我感觉一个一个初始化函数指针数组有点傻
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

struct stuInfo{
    string order;
    string name;
    int grade;
};

stuInfo stu[100000];

bool compare1(stuInfo a,stuInfo b){
    return a.order<b.order;
}
bool compare2(stuInfo a,stuInfo b){
    if(a.name==b.name){
        return a.order<b.order;
    }
    return a.name<b.name;
}
bool compare3(stuInfo a,stuInfo b){
    if(a.grade==b.grade){
        return a.order<b.order;
    }
    return a.grade<b.grade;
}

int main() {
    bool (*compare[3])(stuInfo,stuInfo);
    compare[0]=compare1;
    compare[1]=compare2;
    compare[2]=compare3;


    int n,c;
    while(scanf("%d%d",&n,&c)!=-1){
        for(int i=0;i<n;i++){
            cin>>stu[i].order>>stu[i].name>>stu[i].grade;
        }

        sort(stu,stu+n,compare[c-1]);

        printf("Case:\n");
        for(int i=0;i<n;i++){
            cout<<stu[i].order<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
        }
    }
}


发表于 2023-02-20 17:26:45 回复(0)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
using namespace std;


int C;

struct Student {
    string id;
    string name;
    int score;
};

bool mycmp(Student s1,Student s2) {
    switch (C)
    {
    default:
        break;
    case 1:
        return s1.id < s2.id;
    case 2:
        return s1.name == s2.name ? s1.id < s2.id : s1.name < s2.name;
    case 3:
        return s1.score == s2.score ? s1.id < s2.id : s1.score < s2.score;
    }
    return false;
}


int main() {
    int N;
    while (scanf("%d %d", &N, &C) != EOF) {
        string sid;
        string sname;
        int sscore;
        vector<Student> vec;
        for (int i = 0; i < N; i++) {
            cin >> sid >> sname >> sscore;
            Student s = { sid, sname, sscore };
            vec.push_back(s);
        }

        sort(vec.begin(), vec.end(), mycmp);

        cout << "Case:" << endl;
        for (int i = 0; i < vec.size(); i++) {
            cout << vec[i].id << " " << vec[i].name << " " << vec[i].score << endl;
        }
    }
    return 0;
}
发表于 2021-01-25 11:25:16 回复(0)
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

struct student{
	char id[7];
	char name[9];
	int score;
}stu[100000];
bool cmp1(student a,student b){
	return strcmp(a.id,b.id)<0;
}
bool cmp2(student a,student b){
	if(strcmp(a.name,b.name)!=0) return strcmp(a.name,b.name)<0;
	else return strcmp(a.id,b.id)<0;
}
bool cmp3(student a,student b){
	if(a.score!=b.score)  return a.score<b.score;
	else return strcmp(a.id,b.id)<0;
}
int main(){
	int n,c;
	while(scanf("%d %d",&n,&c)!=EOF){
		int i;
		for(i=0;i<n;i++){
			scanf("%s %s %d",stu[i].id,stu[i].name,&stu[i].score);
		}
		if(c==1) sort(stu,stu+n,cmp1);
		else if(c==2) sort(stu,stu+n,cmp2);
		else if(c==3) sort(stu,stu+n,cmp3);
		printf("Case:\n");
		for(i=0;i<n;i++){
			printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].score);
		}
	}
	return 0;
}

发表于 2021-01-24 18:58:35 回复(0)
Case后面没数字。。又被坑了
#include <stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct{
    int score;
    char name[20];
    char id[20];
}Student,*snode;

int cpr1(const void * a,const void* b)
{
    snode x = (snode) a;
    snode y = (snode) b;
    return strcmp(x->id,y->id);
}
int cpr2(const void * a,const void* b)
{
    snode x = (snode) a;
    snode y = (snode) b;
    if(strcmp(x->name,y->name))
        return strcmp(x->name,y->name);
    else
        return strcmp(x->id,y->id);
}
int cpr3(const void * a,const void* b)
{
    snode x = (snode) a;
    snode y = (snode) b;
    if(x->score != y->score)
        return x->score - y->score;

    else
        return strcmp(x->id,y->id);
}
int main()
{
    int N,C;
    int cnt = 0;
    while(scanf("%d",&N)!=EOF)
    {
        cnt ++;
        scanf("%d",&C);
        snode s = (snode) malloc(sizeof(Student) * N);
        for (snode i = s; i < s + N; i++)
            scanf("%s %s %d", i->id, i->name, &i->score);
        if(C == 1)
            qsort(s, N, sizeof(s[0]), cpr1);
        else if(C == 2)
            qsort(s, N, sizeof(s[0]), cpr2);
        else
            qsort(s, N, sizeof(s[0]), cpr3);

        printf("Case:\n");
        for (snode i = s; i < s + N; i++)
            printf("%s %s %d\n", i->id, i->name, i->score);
    }
    return 0;
}


发表于 2021-01-23 19:41:21 回复(0)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct Student{
    char num[10],name[10];
    int grade;
}stu[100010];

bool cmp1(Student a,Student b){
    return strcmp(a.num,b.num)<0;
}

bool cmp2(Student a,Student b){
    if(strcmp(a.name,b.name)){
        return strcmp(a.name,b.name)<0;
    }else{
        return strcmp(a.num,b.num)<0;
    }
}

bool cmp3(Student a,Student b){
    if(a.grade!=b.grade){
        return a.grade<b.grade;
    }else{
        return strcmp(a.num,b.num)<0;
    }
}

int main(){
    int n,c;
    
    while(scanf("%d %d",&n,&c)!=EOF && n>0){
        for(int i=0;i<n;i++){
            scanf("%s %s %d",stu[i].num,stu[i].name,&stu[i].grade);
        }
        switch (c) {
            case 1:
                sort(stu,stu+n,cmp1);
                break;
            case 2:
                sort(stu,stu+n,cmp2);
                break;
            case 3:
                sort(stu,stu+n,cmp3);
                break;
            default:
                break;
        }
        cout<<"Case "<<c<<":"<<endl;
        for(int i=0;i<n;i++){
            printf("%s %s %d\n",stu[i].num,stu[i].name,stu[i].grade);
        }
    }
}


发表于 2020-07-13 01:47:15 回复(0)
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

typedef struct Student{
    string num;
    string name;
    int score;
    Student(string num,string name,int score):num(num),name(name),score(score){}
}Student;

bool cmp1(Student s1,Student s2){
    return s1.num<s2.num;
}

bool cmp2(Student s1,Student s2){
    if(s1.name!=s2.name)
        return s1.name<s2.name;
    else
        return s1.num<s2.num;
}

bool cmp3(Student s1,Student s2){
    if(s1.score!=s2.score)
        return s1.score<s2.score;
    else
        return s1.num<s2.num;
}

void print(vector<Student> s){
    cout<<"Case:"<<endl;
    for(vector<Student>::iterator it=s.begin();it!=s.end();it++){
        cout<<it->num<<" "<<it->name<<" "<<it->score<<endl;
    }
}

int main(){
    int N,C;
    while(cin>>N&&N){
        cin>>C;
        vector<Student> s;
        string num,name;
        int score;
        while(N--){
            cin>>num>>name>>score;
            s.push_back(Student(num,name,score));
        }
        switch(C){
            case 1:
                sort(s.begin(),s.end(),cmp1);
                print(s);
                break;
            case 2:
                sort(s.begin(),s.end(),cmp2);
                print(s);
                break;
            case 3:
                sort(s.begin(),s.end(),cmp3);
                print(s);
                break;
        }
    }
    return 0;
}

发表于 2020-06-12 14:27:25 回复(0)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
struct Student{
    string id;
    string name;
    int sorce;
}stu[N];

bool cmp1(Student a,Student b){
    return a.id<b.id;
}

bool cmp2(Student a,Student b){
    if(a.name==b.name) return cmp1(a,b);
    else return a.name < b.name;
}

bool cmp3(Student a,Student b){
    if(a.sorce==b.sorce) return cmp1(a,b);
    else return a.sorce < b.sorce;
}

int main(){
    int n,c;
    scanf("%d%d",&n,&c);
    for(int i=0;i<n;i++){
        int sorce;
        string id;
        string name;
        cin>>id>>name>>sorce;
        stu[i] = {id,name,sorce};
    }
    
    if(c==1) sort(stu,stu+n,cmp1);
    else if(c==2) sort(stu,stu+n,cmp2);
    else if(c==3) sort(stu,stu+n,cmp3);
    
    printf("Case:\n");
    for(int i=0;i<n;i++){
        cout<<stu[i].id<<" ";
        cout<<stu[i].name<<" ";
        cout<<stu[i].sorce<<endl;
    }
}

发表于 2020-04-26 23:40:36 回复(0)
使用c++sort函数没任何难度,连PAT里并列排序的问题都没有,学过一遍c++sort函数用法的都懂,
#include<bits/stdc++.h>
using namespace std;
struct student{
    char id[10];
    char name[10];
    int grade;
};
struct student s[100000];
bool cmp1(student s1,student s2){
    return strcmp(s1.id,s2.id)<0;
}
bool cmp2(student s1,student s2){
    return strcmp(s1.name,s2.name)<0;
}
bool cmp3(student s1,student s2){
    if(s1.grade!=s2.grade)return s1.grade<s2.grade;
    else return strcmp(s1.id,s2.id)<0;
}
int main(){
    int n,c;
    while(~scanf("%d %d",&n,&c)){
        if(n==0)break;
        for(int i=0;i<n;i++){
            scanf("%s %s %d",s[i].id,s[i].name,&s[i].grade);
        }
        if(c==1){
            sort(s,s+n,cmp1);
        }else if(c==2){
            sort(s,s+n,cmp2);
        }else if(c==3){
            sort(s,s+n,cmp3);
        }
        printf("Case:\n");
        for(int i=0;i<n;i++){
            printf("%s %s %d\n",s[i].id,s[i].name,s[i].grade);
        }
    }
    return 0;
}

就是题干太长了有点烦人

发表于 2020-03-16 09:44:08 回复(0)

问题信息

难度:
58条回答 9177浏览

热门推荐

通过挑战的用户

查看代码