Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.
3 1<br/>000007 James 85<br/>000010 Amy 90<br/>000001 Zoe 60
000001 Zoe 60<br/>000007 James 85<br/>000010 Amy 90
public static class Student implements Comparable<Student> { String id, name; int grade, column; public Student(String id, String name, int grade, int column) { this.id = id; this.name = name; this.grade = grade; this.column = column; } @Override public int compareTo(Student stu) { if (column == 1) { return id.compareTo(stu.id); } else if (column == 2) { if (name.equals(stu.name)) { return id.compareTo(stu.id); } else { return name.compareTo(stu.name); } } else { if (grade == stu.grade) { return id.compareTo(stu.id); } else { return grade - stu.grade; } } } @Override public String toString() { return id + " " + name + " " + grade; } }
#include <iostream> #include <algorithm> #include <vector> #include <iomanip> using namespace std; class Info { public: int id; string name; int grade; }; bool comp1 (Info i1, Info i2) { return i1.id < i2.id; } bool comp2 (Info i1, Info i2) { if (i1.name != i2.name) return i1.name < i2.name; else return i1.id < i2.id; } bool comp3 (Info i1, Info i2) { if (i1.grade != i2.grade) return i1.grade < i2.grade; else return i1.id < i2.id; } int main () { int n, c; cin >> n >> c; vector<Info> is; is.resize(n); for (int i = 0; i < n; i++) { cin >> is[i].id >> is[i].name >> is[i].grade; } if (c == 1) { sort(is.begin(), is.end(), comp1); } else if (c == 2) { sort(is.begin(), is.end(), comp2); } else if (c == 3) { sort(is.begin(), is.end(), comp3); } for (size_t i = 0; i < is.size(); i++) { cout << setw(6) << setfill('0') << is[i].id << " " << is[i].name << " " << is[i].grade << endl; } return 0; }
a = list(map(int,input().split())) if a[1] == 1: for j in sorted([input() for i in range(a[0])]): print(j) else: for j in sorted([input().split() for i in range(a[0])],\ key = lambda x:(x[1],x[0]) if a[1] - 3 else (int(x[2]),x[0])): print(' '.join(j))
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Student{ int ID; char name[10]; int grade; }stu[100005]; bool cmp1(Student a, Student b){ return a.ID < b.ID; } bool cmp2(Student a, Student b){ if(strcmp(a.name, b.name) != 0) return strcmp(a.name, b.name) < 0; else return a.ID < b.ID; } bool cmp3(Student a, Student b){ if(a.grade != b.grade) return a.grade < b.grade; else return a.ID < b.ID; } int main(void){ int N, C; scanf("%d%d", &N, &C); for(int i = 0; i < N; i++){ scanf("%d%s%d", &stu[i].ID, stu[i].name, &stu[i].grade); } if(C == 1){ sort(stu, stu + N, cmp1); }else if(C == 2){ sort(stu, stu + N, cmp2); }else { sort(stu, stu + N, cmp3); } for(int j = 0; j < N; j++){ printf("%06d %s %d\n", stu[j].ID, stu[j].name, stu[j].grade); } return 0; }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; struct student{ int id,score; char name[10]; }s[100010]; int x,y; bool cmp(student a,student b){ //根据输入的数字1,2或3选择不同的排序方法(所以y要定义为全局变量) if(y==1) return a.id<b.id; else if(y==2){ if(strcmp(a.name,b.name)!=0) return strcmp(a.name,b.name)<0; else return a.id<b.id; } else{ if(a.score!=b.score) return a.score<b.score; else return a.id<b.id; } } int main(){ cin>>x>>y; for(int i=0;i<x;i++) cin>>s[i].id>>s[i].name>>s[i].score; sort(s,s+x,cmp); for(int i=0;i<x;i++) printf("%06d %s %d\n",s[i].id,s[i].name,s[i].score); //注意学号输出要保证六位数字,不足六位填充0 return 0; }提交观点
package go.jacob.day1026; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Demo2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), c = sc.nextInt(); ArrayList<Stu> list = new ArrayList<Stu>(); for (int i = 0; i < n; i++) { list.add(new Stu(sc.next(), sc.next(), sc.nextInt(), c)); } Collections.sort(list); for (Stu s : list) System.out.println(s); } } class Stu implements Comparable<Stu> { String ID; String name; int score; int column; public Stu(String iD, String name, int score, int column) { super(); ID = iD; this.name = name; this.score = score; this.column=column; } @Override public int compareTo(Stu s) { if (column == 1) { return ID.compareTo(s.ID); } else if (column == 2) { if (name.equals(s.name)) return ID.compareTo(s.ID); return name.compareTo(s.name); } else { if (score == s.score) return ID.compareTo(s.ID); return score - s.score; } } // 直接重写toStirng比较好 @Override public String toString() { return ID + " " + name + " " + score; } }
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public int N,C; public Student[] record; public void run() { Scanner in = new Scanner(System.in); N = in.nextInt(); C = in.nextInt(); record = new Student[N]; for(int i = 0; i < N; i++) { record[i] = new Student(in.nextInt(), in.next(), in.nextInt()); } if(C == 1) Arrays.sort(record, new myComparator1()); else if (C == 2) Arrays.sort(record, new myComparator2()); else if(C == 3) Arrays.sort(record, new myComparator3()); for(int i=0; i < N; i++) { String id = record[i].ID + ""; int dif = 6 - id.length(); for(int j = 0; j < dif; j++) id = "0"+id; System.out.println(id+" "+record[i].name+" "+record[i].grade); } in.close(); } public static void main(String[] args) { // TODO Auto-generated method stub Main pat = new Main(); pat.run(); } class Student { public int ID; public String name; public int grade; Student(int ID, String name, int grade) { this.ID = ID; this.name = name; this.grade = grade; } } class myComparator1 implements Comparator<Student> { public int compare(Student a, Student b) { if(a.ID > b.ID) return 1; else if(a.ID < b.ID) return -1; else return 0; } } class myComparator2 implements Comparator<Student> { public int compare(Student a, Student b) { int res = a.name.compareTo(b.name); if (res !=0 ) return res; else { if(a.ID > b.ID) return 1; else if(a.ID < b.ID) return -1; else return 0; } } } class myComparator3 implements Comparator<Student> { public int compare(Student a, Student b) { if(a.grade > b.grade) return 1; else if(a.grade < b.grade) return -1; else { if(a.ID > b.ID) return 1; else if(a.ID < b.ID) return -1; else return 0; } } } }
#include<bits/stdc++.h> using namespace std; const int Max=1e5+10; struct P { int id; char name[10]; int score; } stu[Max]; bool cmp1(P & a,P & b) { return a.id<b.id; } bool cmp2(P & a,P & b) { if(strcmp(a.name,b.name)) { return strcmp(a.name,b.name)<0; } else { return a.id<b.id; } } bool cmp3(P & a,P & b) { if(a.score!=b.score) { return a.score<b.score; } else { return a.id<b.id; } } int main() { int n,m; cin>>n>>m; for(int i=0; i<n; i++) { scanf("%d %s %d",&stu[i].id,stu[i].name,&stu[i].score); } if(m==1) { sort(stu,stu+n,cmp1); } else if(m==2) { sort(stu,stu+n,cmp2); } else { sort(stu,stu+n,cmp3); } for(int i=0;i<n;i++){ printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].score); } return 0; }
#include<iostream> #include<string> #include<map> #include<vector> #include<algorithm> using namespace std; struct line { string v[3]; }; int sortby = 0; bool cmp(line l1, line l2) { if(l1.v[sortby] < l2.v[sortby]) return true; else if (l1.v[sortby] == l2.v[sortby]) return l1.v[0] < l2.v[0]; return false; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int N; cin >> N; cin >> sortby; sortby -= 1; vector<line> v; v.resize(N); for (int i = 0; i < N; ++i) cin >> v[i].v[0] >> v[i].v[1] >> v[i].v[2]; sort(v.begin(), v.end(), cmp); // 卡这个时间可还行... for (int i = 0; i < N; ++i) printf("%s %s %s\n", v[i].v[0].c_str(), v[i].v[1].c_str(), v[i].v[2].c_str()); return 0; }
//个人认为用java的话这道题用面向对象的思想思考更加直白,而不是单纯思考用什么样的数据结构
import java.util.*;
public class newSort {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String[] num=input.nextLine().split(" ");
int loopnum=Integer.parseInt(num[0]);
String colNum=num[1];
ArrayList<Student> data=new ArrayList<Student>();
for(int i=0;i<loopnum;i++){
String[] line=input.nextLine().split(" ");
Student student=new
Student(Integer.parseInt(line[2]),line[1],line[0]);
data.add(student);
}
if(colNum.equals("1")){
Collections.sort(data,new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o1.id.compareTo(o2.id);
}
});
}else if(colNum.equals("2")){
Collections.sort(data,new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
if(o1.name.equals(o2.name)){
return o1.id.compareTo(o2.id);
}else{
return o1.name.compareTo(o2.name);}
}
});
}else if(colNum.equals("3")){
Collections.sort(data,new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
if(o1.grade>o2.grade){
return 1;
}else if(o1.grade<o2.grade){
return -1;
}else{
return o1.id.compareTo(o2.id);
}
// return o1.grade.compareTo(o2.grade);
}
});
}
for(Student student:data){
System.out.println(student.id+" "+student.name+" "+student.grade);
}
}
private static boolean isRepeatInCollection(Collection<?> datas) {
if (datas == null) {// 为空则认为不含重复元素
return false;
}
if (datas instanceof Set) {//如果是set则不含有重复元素
return false;
}
Set<?> noRepeatSet = new HashSet<>(datas);
return !(datas.size() == noRepeatSet.size());
}
}
class Student{
public int grade;
public String name;
public String id;
public Student(int grade, String name, String id){
this.grade=grade;
this.name=name;
this.id=id;
}
}
#include<bits/stdc++.h> using namespace std; struct Rec{ int id,grade; char name[10]; }; Rec rec[100010]; bool com1(Rec r1,Rec r2){ return r1.id<r2.id; } bool com2(Rec r1,Rec r2){ if(strcmp(r1.name,r2.name)<0) return true; else if(strcmp(r1.name,r2.name)==0) return r1.id < r2.id; else return false; } bool com3(Rec r1,Rec r2){ if(r1.grade!=r2.grade) return r1.grade < r2.grade; else return r1.id < r2.id; } int main(){ int i,n,c; scanf("%d %d",&n,&c); for(i=0;i<n;i++) scanf("%d %s %d",&rec[i].id,rec[i].name,&rec[i].grade); switch(c){ case 1:sort(rec,rec+n,com1);break; case 2:sort(rec,rec+n,com2);break; case 3:sort(rec,rec+n,com3);break; } for(i=0;i<n;i++) printf("%06d %s %d\n",rec[i].id,rec[i].name,rec[i].grade); return 0; }
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
ArrayList<student> list = new ArrayList<>();
int sortNum = sc.nextInt();
for (int i = 0;i<n;i++){ list.add(new student(sc.next(),sc.next(),sc.nextInt())); } Collections.sort(list, new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
if (sortNum == 1){
return o1.id.compareTo(o2.id);
}else if (sortNum == 2){
if (o1.name.equals(o2.name)){
return o1.id.compareTo(o2.id);
}else {
return o1.name.compareTo(o2.name);
}
}else {
if (o1.score == o2.score){
return o1.id.compareTo(o2.id);
}else {
return o1.score - o2.score;
}
}
}
});
for (student s:list){
System.out.println(s.id+" "+s.name+" "+s.score);
}
}
}
}
class student{
String id;
String name;
int score;
public student(String id,String name,int score){
this.id = id;
this.name = name;
this.score = score;
}
}
思路:选择进行sort。 #include <iostream> #include <string> #include <vector> #include <fstream> #include <algorithm> using namespace std; struct stu { string ID; string name; int grade; //stu(string id, string na, int gra) :ID(id), name(na), grade(gra) {}; }; bool Cmp1(struct stu & a, struct stu & b) { return a.ID < b.ID; } bool Cmp2(struct stu & a, struct stu & b) { if (a.name != b.name) return a.name < b.name; else return a.ID < b.ID; } bool Cmp3(struct stu & a, struct stu & b) { if (a.grade != b.grade) return a.grade < b.grade; else return a.ID < b.ID; } int main() { //ifstream cin("test.txt"); int n, c; while (cin >> n >> c) { string name, ID; int grade; vector<struct stu> v(n); for (int i = 0; i < n; i++) { cin >> ID >> name >> grade; v[i].ID = ID; v[i].name = name; v[i].grade = grade; } if(c == 1) sort(v.begin(), v.end(), Cmp1); else if(c == 2) sort(v.begin(), v.end(), Cmp2); else sort(v.begin(), v.end(), Cmp3); for (int i = 0; i < v.size(); i++) { cout << v[i].ID << " " << v[i].name <<" "<< v[i].grade << endl; } } system("pause"); }
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; struct Student{ int score; string id, name; }; int cmp1(Student a, Student b){ return a.id < b.id; } int cmp2(Student a, Student b){ return a.name < b.name || (a.name==b.name && a.id<b.id); } int cmp3(Student a, Student b){ return a.score < b.score || (a.score==b.score && a.id<b.id); } int main(){ int N, C; vector<Student> stu; cin>>N; cin>>C; for(int i=0; i<N; ++i){ int sc; string id, name; Student s; cin>>id>>name>>sc; s.id = id, s.name = name, s.score = sc; stu.push_back(s); } // sort if(C==1) sort(stu.begin(), stu.end(), cmp1); else if(C==2) sort(stu.begin(), stu.end(), cmp2); else sort(stu.begin(), stu.end(), cmp3); // print sorted result for(int i=0; i<stu.size(); ++i){ cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].score<<endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; class Student{ private: string s_no; string s_name; int s_grade; public: void set_value(string no,string name, int grade){ s_no = no; s_name = name; s_grade = grade; }; void show(){ cout << s_no << " " << s_name << " " << s_grade << endl; }; string show_no(){ return s_no; }; string show_name(){ return s_name; }; int show_grade(){ return s_grade; }; }; bool compare1(Student s1, Student s2){ return s1.show_no() < s2.show_no(); } bool compare2(Student s1, Student s2){ if(s1.show_name() != s2.show_name()) return s1.show_name() < s2.show_name(); return s1.show_no() < s2.show_no(); } bool compare3(Student s1, Student s2){ if(s1.show_grade() != s2.show_grade()) return s1.show_grade() < s2.show_grade(); return s1.show_no() < s2.show_no(); } int main(){ int N, C; vector<Student> v; scanf("%d %d", &N, &C); while(N--){ Student s; int grade; string no, name; cin >> no >> name >> grade; s.set_value(no, name, grade); v.push_back(s); } if(C == 1) sort(v.begin(), v.end(), compare1); else if(C == 2) sort(v.begin(), v.end(), compare2); else sort(v.begin(), v.end(), compare3); for(auto iter=v.begin();iter!=v.end();iter++) iter->show(); return 0; }
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef struct { char name[8],Id[6];int grade; }stu; stu s[100000]; bool cmp1(stu a,stu b) { return strcmp(a.Id,b.Id)<0; } bool cmp2(stu a,stu b) { if(!strcmp(a.name,b.name)) return strcmp(a.Id,b.Id)<0; else return strcmp(a.name,b.name)<0; } bool cmp3(stu a,stu b) { if(a.grade==b.grade) return strcmp(a.Id,b.Id)<0; else return a.grade<b.grade; } int main() { int n,i,k; while(~scanf("%d%d",&n,&k)) { for(i=0;i<n;i++) scanf("%s%s%d",s[i].Id,s[i].name,&s[i].grade); if(k==1) sort(s,s+n,cmp1);if(k==2) sort(s,s+n,cmp2);if(k==3) sort(s,s+n,cmp3); for(i=0;i<n;i++) printf("%s %s %d\n",s[i].Id,s[i].name,s[i].grade); } return 0; } //这个在pat的OJ上是不通过的,但是在牛客网上就是通过的,请问为啥?