测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。 每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。
将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。 然后输出学生信息,按照如下格式: 姓名 年龄 成绩 学生姓名的字母序区分字母的大小写,如A要比a的字母序靠前(因为A的ASC码比a的ASC码要小)。
3 abc 20 99 bcd 19 97 bed 20 97
bcd 19 97 bed 20 97 abc 20 99
#include<iostream> #include<algorithm> #include<string> using namespace std; struct data { string name;//直接用string类便于姓名的比较 int age; int grades; bool operator<(const data&a)//重载<运算符 { if(grades!=a.grades)return grades<a.grades; else if(name!=a.name)return name<a.name; else return age<a.age; } }buf[1001]; int main() { int n; while(cin>>n) { for(int i=0;i<n;i++)cin>>buf[i].name>>buf[i].age>>buf[i].grades; sort(buf,buf+n); for(int i=0;i<n;i++)cout<<buf[i].name<<buf[i].age<<buf[i].grades<<endl; } return 0; }
#include<iostream> #include<cstring> #include<algorithm> using namespace std; //自定义排序的思想 typedef struct Student { char name[101]; int age; int score; }; bool operator<(const Student & s_1,const Student & s_2) { int c = strcmp(s_1.name, s_2.name);//比较字符串大小,相等为0,s_1比s_2小为负数,否则为正 if(s_1.score == s_2.score) { if(c == 0) return s_1.age < s_2.age; else return c < 0; } else return s_1.score < s_2.score; } int main(void) { int n; Student student[1001]; while(cin >> n) { for(int i = 0;i < n;i++) { cin >> student[i].name; int age; cin >> age; student[i].age = age; int score; cin >> score; student[i].score = score; } sort(student, student + n); for(int i = 0;i < n;i++) { cout << student[i].name << ' ' << student[i].age << ' ' << student[i].score; cout << endl; } } return 0; }
#include<stdio.h>//思想1.先按照分数排序2.如果分数相同在按照姓名3.再相同则按照年龄 #include<string.h> struct Student{ char name[10];int age;int score; }stu[1000]; int main() { int n,i,j;scanf("%d",&n); for(i=0;i<n;i++) scanf("%s%d%d",stu[i].name,&stu[i].age,&stu[i].score); int ag,sc;char na[10];//1.先按成绩排序 for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) if(stu[j].score>stu[j+1].score) {//全部交换 strcpy(na,stu[j].name);ag=stu[j].age;sc=stu[j].score; strcpy(stu[j].name,stu[j+1].name);stu[j].age=stu[j+1].age;stu[j].score=stu[j+1].score; strcpy(stu[j+1].name,na);stu[j+1].age=ag;stu[j+1].score=sc; } //2.成绩相同按照姓名排序3.姓名还是相同按年龄排序 for(i=0;i<n-1;i++)//注意这里也用排序, 只不过排序的时候加了条件只有成绩相同的时候在判断是否要从新排序 for(j=0;j<n-1-i;j++) if(stu[j].score==stu[j+1].score)//成绩相同 { if(strcmp(stu[j].name,stu[j+1].name)>0) {//交换 strcpy(na,stu[j].name);ag=stu[j].age;sc=stu[j].score; strcpy(stu[j].name,stu[j+1].name);stu[j].age=stu[j+1].age;stu[j].score=stu[j+1].score; strcpy(stu[j+1].name,na);stu[j+1].age=ag;stu[j+1].score=sc; } if(strcmp(stu[j].name,stu[j+1].name)==0)//姓名相同 { if(stu[j].age>stu[j+1].age) {//交换 strcpy(na,stu[j].name);ag=stu[j].age;sc=stu[j].score; strcpy(stu[j].name,stu[j+1].name);stu[j].age=stu[j+1].age;stu[j].score=stu[j+1].score; strcpy(stu[j+1].name,na);stu[j+1].age=ag;stu[j+1].score=sc; } } } for(i=0;i<n;i++)//输出 printf("%s %d %d\n",stu[i].name,stu[i].age,stu[i].score); }
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(); Student[] arr = new Student[n]; for (int i = 0; i < n; i++) arr[i] = new Student(scanner.next(),scanner.nextInt(),scanner.nextInt()); Arrays.sort(arr); for (Student student : arr) System.out.println(student); } } static class Student implements Comparable<Student>{ String name; Integer age; Integer score; public Student(String name, Integer age, Integer score) { this.name = name; this.age = age; this.score = score; } //将学生数据按成绩从低到高排序,如果成绩相同则按姓名字符的字典序由小到大排序,如果姓名的字典序也相同则按照学生的年龄从小到大排序 @Override public int compareTo(Student o) { if (this.score.compareTo(o.score)==0){ if (this.name.compareTo(o.name)==0) return this.age.compareTo(o.age); else return this.name.compareTo(o.name); }else return this.score.compareTo(o.score); } //姓名 年龄 成绩 @Override public String toString() { return name+" "+age+" "+score ; } } }
#include<bits/stdc++.h> using namespace std; struct student{ char name[100]; int age; int score; }; student s[1000]; bool cmp(student x,student y){ if(x.score!=y.score) return x.score<y.score; else if(x.score==y.score&&strcmp(x.name,y.name)!=0) return strcmp(x.name,y.name)<0; else return x.age<y.age; } int main(){ int N; while(cin>>N){ for(int i=0;i<N;i++){ cin>>s[i].name>>s[i].age>>s[i].score; } sort(s,s+N,cmp); for(int i=0;i<N;i++){ cout<<s[i].name<<" "<<s[i].age<<" "<<s[i].score<<endl; } } return 0; }
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
class Student {
private String name;
private int age;
private int grade;
public Student(String name, int age, int grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getGrade() {
return grade;
}
public String toString() {
return name + " " + age + " " + grade;
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
List list = new ArrayList(n);
for (int i = 0; i < n; i++) {
String name = scan.next();
int age = scan.nextInt();
int grade = scan.nextInt();
list.add(new Student(name, age, grade));
}
Collections.sort(list, new MyComparator());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
}
}
private static class MyComparator implements Comparator {
@Override
public int compare(Student o1, Student o2) {
if (o1.getGrade() != o2.getGrade()) {
return o1.getGrade() > o2.getGrade() ? 1 : -1;
} else if (o1.getName().compareTo(o2.getName()) != 0) {
if (o1.getName().compareTo(o2.getName()) > 0) {
return 1;
} else {
return -1;
}
} else {
return o1.getAge() - o2.getAge();//默认升序
}
}
}
}
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct stu {
string name;
int age;
int score;
bool operator <(const stu &A) {
if (score < A.score) {
return 1;
}
else if (score == A.score) {
if (name < A.name) {
return 1;
}
else if (name == A.name) {
if (age < A.age) {
return 1;
}
}
}
return 0;
}
}s[1001];
int main() {
int n;
while (cin >> n) {
for (int i = 0; i < n; i++) {
cin >> s[i].name;
cin >> s[i].age;
cin >> s[i].score;
}
sort(s,s+n);
for (int i = 0; i < n; i++) {
cout << s[i].name << " " << s[i].age << " " << s[i].score << endl;
}
}
return 0;
}
#include<string.h> struct Stu{char name[101];int age,grade;}stus[1000]; int cmp(const void* a,const void* b){ struct Stu* s1 = (struct Stu*)a;struct Stu* s2 = (struct Stu*)b; return s1->grade != s2->grade?s1->grade - s2->grade:(strcmp(s1->name,s2->name)!=0?strcmp(s1->name,s2->name):s1->age - s2->age); } main(){ int n,i; for(;~scanf("%d",&n);){ for (i = 0; i < n &&~scanf("%s %d %d",stus[i].name,&stus[i].age,&stus[i].grade); i++); qsort(stus,n,sizeof(struct Stu),cmp); for (i = 0; i < n && printf("%s %d %d\n",stus[i].name,stus[i].age,stus[i].grade); i++); } }
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; typedef struct { string name; int age, socre; }student; bool cmpsocre(student x, student y) { if (x.socre == y.socre && x.name == y.name) return x.age < y.age; else if (x.socre == y.socre && x.name != y.name) return x.name < y.name; else return x.socre < y.socre; } int main() { int length; while (cin >> length) { vector<student> data; student s; for (int i = 0; i < length; i++) { cin >> s.name >> s.age >> s.socre; data.push_back(s); } sort(data.begin(), data.end(), cmpsocre); for (int i = 0; i < length; i++) cout << data[i].name << " " << data[i].age << " " << data[i].socre << endl; } return 0; }
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct student{ char name[110]; int age; int grade; }; bool cmp(student a,student b){ if(a.grade==b.grade){ if(strcmp(a.name,b.name)==0)return a.age<b.age; else return strcmp(a.name,b.name)<0; } else return a.grade<b.grade; } int main(){ int N,n; student stu[1001]; while(scanf("%d",&N)!=EOF){ for(int i=0;i<N;i++){ scanf("%s %d %d",stu[i].name,&stu[i].age,&stu[i].grade); } sort(stu,stu+N,cmp); for(n=0;n<N-1;n++){ printf("%s %d %d\n",stu[n].name,stu[n].age,stu[n].grade); } printf("%s %d %d",stu[n].name,stu[n].age,stu[n].grade); } return 0; }
#include <iostream> #include <vector> #include <string.h> #include <algorithm> using namespace std; struct student{ char name[100]; int age; int score; }; bool cmp(const student s1,const student s2){ if(s1.score!=s2.score) return s1.score<s2.score; else if(strcmp(s1.name,s2.name)!=0) return strcmp(s1.name,s2.name)<0; else return s1.age<s2.age; } int main(){ int n; while(cin>>n){ int i; vector<student> students(n); for(i=0;i<n;i++) cin>>students[i].name>>students[i].age>>students[i].score; sort(students.begin(),students.end(),cmp); for(i=0;i<n;i++) cout<<students[i].name<<" "<<students[i].age<<" "<<students[i].score<<endl; } return 0; }
Object C
#include<stdio.h>
#include<stdlib.h>
typedef struct Student{
char name[110];
int age, grade;
}Student;
Student stu[1010];
int cmp(const void *a, const void *b){
struct Student *s1 = (Student *) a;
struct Student *s2 = (Student *) b;
if(s1->grade != s2->grade) return s1->grade-s2->grade;
if(strcmp(s1->name, s2->name)) return strcmp(s1->name, s2->name);
return s1->age-s2->age;
}
int main(){
//freopen("aa", "r", stdin);
int n, i;
while(~scanf("%d", &n)){
for(i = 0; i<n; ++i){
scanf("%s %d %d", stu[i].name, &stu[i].age, &stu[i].grade);
}
qsort(stu, n, sizeof(stu[0]), cmp);
for(i = 0; i<n; i++)
printf("%s %d %d\n", stu[i].name, stu[i].age, stu[i].grade);
}
return 0;
}
真男人就要手写快排算法。
#include <bits/stdc++.h> using namespace std; struct Student { int score; string name; int age; bool operator == (const Student &s) const { return score == s.score && name == s.name && age == s.age; } bool operator < (const Student &s) const { if (this->score != s.score) return this->score < s.score; else if (this->name != s.name) return this->name < s.name; else return this->age < s.age; } bool operator <= (const Student &s) const { return *this < s || *this == s; } }; Student sarr[1000]; int Partition(int first, int last) { Student pVal = sarr[first]; while (first < last) { while (first < last && pVal <= sarr[last]) --last; sarr[first] = sarr[last]; while (first < last && sarr[first] <= pVal) ++first; sarr[last] = sarr[first]; } sarr[first] = pVal; return first; } void QuickSort(int beg, int end) { if (beg + 1 < end) { int pivot = Partition(beg, end - 1); QuickSort(beg, pivot); QuickSort(pivot + 1, end); } } int main() { int n; while (cin >> n) { for (int i = 0; i < n; ++i) cin >> sarr[i].name >> sarr[i].age >> sarr[i].score; QuickSort(0, n); for (int i = 0; i < n; ++i) cout << sarr[i].name << " " << sarr[i].age << " " << sarr[i].score << endl; } return 0; }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int len=scanner.nextInt(); scanner.nextLine(); Student [] students=new Student[len]; for (int i=0;i<len;i++){ Student temp=new Student(); String str=scanner.next(); int age=scanner.nextInt(); int score=scanner.nextInt(); temp.setName(str); temp.setAge(age); temp.setScore(score); students[i]=temp; } Student_sort sort=new Student_sort(students); sort.bubbleSort(); sort.print(); } } class Student_sort{ private Student [] students; public Student_sort(Student [] students) { this.students=students; } public void bubbleSort(){ for (int i=1;i<students.length;i++){ for (int j=0;j<students.length-i;j++){ if (students[j].getScore()>students[j+1].getScore()){//比较分数 Student temp=students[j]; students[j]=students[j+1]; students[j+1]=temp; }else if (students[j].getScore()==students[j+1].getScore()){//如果分数相同 if (students[j].getName().length()>students[j+1].getName().length()){//比较字符串长度 Student temp=students[j]; students[j]=students[j+1]; students[j+1]=temp; }else if (students[j].getName().length()==students[j+1].getName().length()){//如果字符串长度相等 int count1=0; int count2=0; for (int m=0;m<students[j].getName().length();m++){ count1+=students[j].getName().charAt(m)-'0'; count2+=students[j+1].getName().charAt(m)-'0'; } if (count1>count2){ Student temp=students[j]; students[j]=students[j+1]; students[j+1]=temp; }else if (count1==count2){ if (students[j].getAge()>students[j+1].getAge()){//比较年龄 Student temp=students[j]; students[j]=students[j+1]; students[j+1]=temp; } } } } } } } public void print(){ for (int i=0;i<students.length;i++){ System.out.println(students[i].getName()+" "+students[i].getAge()+" "+students[i].getScore()); } } } class Student{ private String name; private int age; private int score; public Student() { } 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; } }