- 有N個學生的資料,将學生資料按成績高低排序,如果成績相同則按姓名字元的字母序排序,如果姓名的字母序也相同則按照學生的年齡排序,并輸出N個學生排序後的資訊。
題目位址:http://ac.jobdu.com/problem.php?pid=1061
題目描述:
- 輸入:
-
測試資料有多組,每組輸入第一行有一個整數N(N<=1000),接下來的N行包括N個學生的資料。
每個學生的資料包括姓名(長度不超過100的字元串)、年齡(整形數)、成績(小于等于100的正數)。
- 輸出:
-
将學生資訊按成績進行排序,成績相同的則按姓名的字母序進行排序。
然後輸出學生資訊,按照如下格式:
姓名 年齡 成績
- 樣例輸入:
-
3 abc 20 99 bcd 19 97 bed 20 97
- 樣例輸出:
-
bcd 19 97 bed 20 97 abc 20 99
- 提示:
- 學生姓名的字母序區分字母的大小寫,如A要比a的字母序靠前(因為A的ASC碼比a的ASC碼要小)。
- 來源:
- 2000年清華大學計算機研究所學生機試真題
-
最典型的排序問題,C++使用sort,Java使用Arrays.sort。
上代碼
- C++ AC
-
Java AC#include <stdio.h> #include <algorithm> #include<string.h> using namespace std; const int maxn = 1002; struct Student{ char name[102]; int age; int score; }students[maxn]; int n , c , i; bool cmp(Student s1 , Student s2){ if(s1.score == s2.score){ int tmp = strcmp(s1.name , s2.name); if(tmp == 0){ return s1.age < s2.age; }else{ return tmp < 0; } }else { return s1.score < s2.score; } } int main(){ while(scanf("%d",&n) != EOF){ for(i = 0; i < n; i++){ scanf("%s %d %d",&students[i].name,&students[i].age,&students[i].score); } sort(students, students+n, cmp); for(i = 0; i < n; i++){ printf("%s %d %d\n",students[i].name,students[i].age,students[i].score); } } return 0; } /************************************************************** Problem: 1061 User: wangzhenqing Language: C++ Result: Accepted Time:90 ms Memory:1144 kb ****************************************************************/
-
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Arrays; public class Main { /* * 1061 */ public static void main(String[] args) throws Exception{ StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); while (st.nextToken() != StreamTokenizer.TT_EOF) { int N = (int)st.nval; Student[] students = new Student[N]; for (int i = 0; i < N; i++) { st.nextToken(); String name = st.sval; st.nextToken(); int age = (int)st.nval; st.nextToken(); int score = (int)st.nval; Student student = new Student(name, age, score); students[i] = student; } Arrays.sort(students); for (int i = 0; i < N; i++) { System.out.println(students[i].getName()+" "+ students[i].getAge()+" "+students[i].getScore()); } } } } class Student implements Comparable<Student>{ private String name ; private int age ; private int score; 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; } public Student(String name, int age, int score) { super(); this.name = name; this.age = age; this.score = score; } public int compareTo(Student o) { if (o.getScore()!=this.getScore()) { return this.getScore() - o.getScore() ; }else { if (!o.getName().equals(this.getName())) { return this.getName().compareTo(o.getName()); }else { return this.getAge()-o.getAge(); } } } } /************************************************************** Problem: 1061 User: wangzhenqing Language: Java Result: Accepted Time:1590 ms Memory:107776 kb ****************************************************************/