测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。
对每个测试用例,首先输出1行“Case:”。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
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);
}
} #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;
} #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;
}
感激大佬解答 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()
#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;
} 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;
}
}
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool cmp1(pair<pair<string, string>, int> pi1,
pair<pair<string, string>, int> pi2) {
return pi1.first.first < pi2.first.first;
}
bool cmp2(pair<pair<string, string>, int> pi1,
pair<pair<string, string>, int> pi2) {
if (pi1.first.second == pi2.first.second) {
return pi1.first.first < pi2.first.first;
}
return pi1.first.second < pi2.first.second;
}
bool cmp3(pair<pair<string, string>, int> pi1,
pair<pair<string, string>, int> pi2) {
if (pi1.second == pi2.second) {
return pi1.first.first < pi2.first.first;
}
return pi1.second < pi2.second;
}
int main() {
long long int N, C;
cin >> N >> C;
vector<pair<pair<string, string>, int>> pis;
string no, name;
int score;
for (int i = 0; i < N; ++i) {
cin >> no >> name >> score;
pis.push_back({{no, name}, score});
}
if (C == 1)sort(pis.begin(), pis.end(), cmp1);
else if (C == 2)sort(pis.begin(), pis.end(), cmp2);
else sort(pis.begin(), pis.end(), cmp3);
cout << "Case:" << endl;
for (int i = 0; i < N; ++i) {
cout << pis[i].first.first << " " << pis[i].first.second
<< " " << pis[i].second << endl;
}
return 0;
} #include <iostream>
#include <utility>
using namespace std;
#include "vector"
#include "algorithm"
struct Student{
string id;
string name;
int score;
Student()=default;
Student(string id,string name,int score):id(std::move(id)),name(std::move(name)),score(score){}
};
bool cmp1(Student s1,Student s2){
return s1.id<s2.id;
}
bool cmp2(Student s1,Student s2){
return s1.name<s2.name;
}
bool cmp3(Student s1,Student s2){
if(s1.name==s2.name||s1.score==s2.score){
return s1.id<s2.id;
}
return s1.score<s2.score;
}
int main() {
int N,C;
while(cin>>N>>C){
if(N==0) break;
vector<Student>students(N);
for(int i=0;i<N;i++){
string id;
string name;
int score;
cin>>id>>name>>score;
students[i]=Student(id,name,score);
}
if(C==1)
sort(students.begin(),students.end(),cmp1);
else if(C==2)
sort(students.begin(),students.end(),cmp2);
else if(C==3)
sort(students.begin(),students.end(),cmp3);
cout<<"Case:"<<endl;
for(auto a:students){
cout<<a.id<<" "<<a.name<<" "<<a.score<<endl;
}
}
} 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;
}
} #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;
} #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;
} #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;
}
}
#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;
}
}
} #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;
}
#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;
} #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;
}
#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);
}
}
}
#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;
}