Title Description:
The data of N students are sorted according to their grades. If the scores are the same, they are sorted according to the alphabetical order of the name characters. If the alphabetical order of the names is the same, they are sorted according to the age of the students, and the sorted information of N students is output.
Input:
There are several groups of test data, each group has an integer N (N<=1000) in the first line of input, and the next N line includes data of N students.
Each student's data includes name (string length not exceeding 100), age (integer), and grade (positive number less than or equal to 100).
Output:
Students'information is sorted according to their grades, and those with the same grades are sorted according to the alphabetical order of their names.
Then output the student information in the following format:
Name and Age Achievements
Sample input:
3
abc 20 99
bcd 19 97
bed 20 97
Sample output:
bcd 19 97
bed 20 97
abc 20 99
Tips:
The alphabetical order of a student's name distinguishes between upper and lower case letters. For example, A is ahead of a's alphabetical order (because the ASC code of A is smaller than the ASC code of a).
A very basic question, which I used to do with C++AC before, is now redone in Java.
import java.util.Scanner;
class Student{
String name;
int age;
int score;
void swap(Student s){
String min = s.name;
s.name = this.name;
this.name = min;
this.age = s.age^this.age;
s.age = s.age^this.age;
this.age = s.age^this.age;
this.score = s.score^this.score;
s.score = s.score^this.score;
this.score = s.score^this.score;
}
void compare(Student s){
if(s.score < this.score){
this.swap(s);
}else if(s.score == this.score && this.name.compareTo(s.name) > 0){
this.swap(s);
}else if(s.score == this.score && this.name.compareTo(s.name) == 0 && s.age < this.age){
this.swap(s);
}
}
void set(Student s){
this.score = s.score;
this.name = s.name;
this.age = s.age;
}
}
public class Main {
public static void main(String args[]){
Student[] s = new Student[1000];
Scanner sc = new Scanner(System.in);
int n;
while(sc.hasNext()){
n = sc.nextInt();
for(int i=0;i<n;i++){
s[i] = new Student();
s[i].name = sc.next();
s[i].age = sc.nextInt();
s[i].score = sc.nextInt();
}
for(int i=0;i<n;i++){
Student min = new Student();
min.set(s[i]);;
for(int j=i;j<n;j++){
min.compare(s[j]);
}
s[i].set(min);
}
for(int i=0;i<n;i++){
System.out.println(s[i].name + " " + s[i].age + " " + s[i].score);
}
}
sc.close();
}
}
/**************************************************************
Problem: 1061
User: zzuljs
Language: Java
Result: Accepted
Time:1600 ms
Memory:93408 kb
****************************************************************/
Sorting is written by oneself, using selection sort, comparing low
Seeing others sort with Comparable interface, I feel very tall. Learn about it.——
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: zzuljs
Language: Java
Result: Accepted
Time:1510 ms
Memory:102136 kb
****************************************************************/
It seems that the pipeline flow input is faster?
However, it is obvious that sorting with Array templates takes up more space and time, which should be noted.