输入第一行包括一个整数N(1<=N<=100),代表学生的个数。 接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。 如果学生的成绩相同,则按照学号的大小进行从小到大排序。
3 1 90 2 87 3 92
2 87 1 90 3 92
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayList<Student> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(new Student(scanner.nextInt(),scanner.nextInt())); Collections.sort(list); for (Student student : list) System.out.println(student); } public static class Student implements Comparable<Student>{ Integer id; Integer score; public Student(Integer id,Integer score){ this.id=id; this.score=score; } @Override public int compareTo(Student o) { return this.score.equals(o.score) ?id.compareTo(o.id):score.compareTo(o.score); } @Override public String toString() { return id+" "+score; } } }解法二:
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(scanner.nextLine()); list.sort((o1, o2) -> { String[] s1 = o1.split(" "); Integer id1 = Integer.parseInt(s1[0]); Integer score1= Integer.parseInt(s1[1]); String[] s2 = o2.split(" "); Integer score2= Integer.parseInt(s2[1]); Integer id2 = Integer.parseInt(s2[0]); return score1.equals(score2) ?id1.compareTo(id2):score1.compareTo(score2); }); for (String s : list) System.out.println(s); } }
#include<stdio.h>> int main(){ int n,a[200],i,j,k; while(~scanf("%d",&n)){ for(i=0;i<2*n;i++) scanf("%d",&a[i]); for(i=1;i<(2*n-2);i+=2){ for(j=i+2;j<2*n;j+=2){ if(a[j]<=a[i]){ k=a[j-1]; a[j-1]=a[i-1]; a[i-1]=k; k=a[j]; a[j]=a[i]; a[i]=k; } } } for(i=0;i<2*n;i++){ printf("%d ",a[i]); if((i+1)%2==0) printf("\n"); } } }
/* *定义cmp ,vector排序。 */ #include<bits/stdc++.h> using namespace std; vector<pair<int,int> > v; int n; bool cmp(pair<int, int> a, pair<int, int> b) { if(a.second == b.second) return a.first < b.first; return a.second < b.second; } int main() { int x, g; while(scanf("%d",&n) == 1) { for(int i = 0;i < n; i++) { scanf("%d%d",&x,&g); v.push_back(make_pair(x, g)); } sort(v.begin(), v.end(), cmp); for(int i = 0;i < n; i++) { printf("%d %d\n",v[i].first,v[i].second); } } return 0; }
#include<iostream> using namespace std; struct student{ int id; int grade; }; int main(){ student stu[100]; int N; cin>>N; for(int i=0;i<N;i++){ cin>>stu[i].id>>stu[i].grade; } for(int i=0;i<N-1;i++){ for(int j=0;j<N-i-1;j++){ if(stu[j].grade>stu[j+1].grade){ student item=stu[j]; stu[j]=stu[j+1]; stu[j+1]=item; } if(stu[j].grade==stu[j+1].grade){ if(stu[j].id>stu[j+1].id){ student item=stu[j]; stu[j]=stu[j+1]; stu[j+1]=item; } } } } for(int i=0;i<N;i++){ cout<<stu[i].id<<" "<<stu[i].grade<<endl; } return 0; }
/*STL就是好用*/ #include<iostream> (720)#include<algorithm> #include<vector> using namespace std; struct student { int id; int grade; }; bool cmp(student a,student b) { return (a.grade==b.grade)?(a.id<b.id):(a.grade<b.grade); } int main() { int n,i; while(cin>>n) { vector<student> ve(n); for(i=0;i<n;i++) { cin>>ve[i].id>>ve[i].grade; } sort(ve.begin(),ve.end(),cmp); for(i=0;i<n;i++) { cout<<ve[i].id<<" "<<ve[i].grade<<endl; } } return 0; }
用一个二维数组记录;score_record[i][j].
score_record[i][]记录成绩等于i的学生的学号,学号按照从小到的顺序排列,使用插入排序。
#include<stdio.h> void swap(int *a,int *b) { int temp; temp=*a; *a=*b; *b=temp; } int main(){ int score_record[101][101]={0}; int N,i,j,id,score,index; while(scanf("%d",&N) != EOF){ for(i=0; i<N; i++){ scanf("%d%d",&id,&score); if(score_record[score][0] == 0){ score_record[score][0] = 1; score_record[score][1] = id; } else{ index = ++score_record[score][0]; score_record[score][index] = id; while(index>1){ if(score_record[score][index]<score_record[score][index-1]){ swap(&score_record[score][index], &score_record[score][index-1]); index--; } else{ break; } } } } for(score=0; score<101; score++){ for(j=1; j<=score_record[score][0]; j++){ printf("%d %d\n",score_record[score][j],score); } } } }
// 定义学生类,重载<号 // 使用STL的动态数组vector,和sort函数 #define _CRT_SECURE_NO_WARNINGS #include<algorithm> #include<cstdio> #include<vector> using namespace std; struct Stu { int no; int grade; Stu(int no, int grade) :no(no), grade(grade) {} int operator < (Stu b) { if (this->grade != b.grade) return this->grade < b.grade; return this->no < b.no; } }; int main() { int n, no, grade; vector<Stu> v; while (scanf("%d", & n) == 1) { v.clear(); for (int i = 0; i < n; i++) { scanf("%d%d", &no, &grade); v.push_back(Stu(no, grade)); } sort(v.begin(), v.begin() + n); for (int i = 0; i < n; i++) { printf("%d %d\n", v[i].no, v[i].grade); } } return 0; }
#include<stdio.h>
#include<malloc.h>
void swap(int* a, int* b){
int temp = 0;
temp = *a;
*a = *b;
*b = temp;
}
int main(){
int n = 0; //人数
while(scanf("%d", &n) != EOF){
int *number = (int *) malloc (sizeof(int) * n); //学号
int *grade = (int *) malloc (sizeof(int) * n); //成绩
for(int i = 0; i < n; i++){
scanf("%d", &number[i]);
scanf("%d", &grade[i]);
}
for(int i = 0; i < n - 1; i++)
for(int j = 0; j < n - 1 - i; j++){ //冒泡排序
if(grade[j] > grade[j + 1]){
swap(&grade[j], &grade[j + 1]);
swap(&number[j], &number[j + 1]);
}
if(grade[j] == grade[j + 1])
if(number[j] > number[j + 1]){
swap(&grade[j], &grade[j + 1]);
swap(&number[j], &number[j + 1]);
}
}
for(int i = 0; i < n; i++)
printf("%d %d\n", number[i], grade[i]);
free(number);
free(grade);
}
return 0;
}
#include<vector> #include<iostream> using namespace std; int main() { int n; cin>>n; vector<int> v(101,101);//(学号,成绩) for(int i=0;i<n;i++){ int p,q; cin>>p>>q; v[p]=q; } for(int j=1;j<101;j++){ for(int k=0;k<v.size();k++){ if(v[k]==j){ cout<<k<<' '<<j<<endl; } } } return 0; }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String... args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int cases = sc.nextInt(); List<String> list = new ArrayList<>(cases); while (cases-- != 0) list.add(sc.next() + "-" + sc.next()); list.sort((a, b) -> Integer.parseInt(a.substring(0, a.indexOf("-"))) - Integer.parseInt(b.substring(0, b.indexOf("-")))); list.sort((a, b) -> a.substring(a.indexOf("-") + 1).compareTo(b.substring(b.indexOf("-") + 1))); for (String s : list) System.out.println(s.replace("-", " ")); } sc.close(); } }
#include <stdio.h> #include <stdlib.h> typedef struct student{ int num; int score; }student; int cmp(const void *a,const void *b){ if ((*(student *)a).score > (*(student *)b).score) return 1;// 分数大的在后面,1表示需要调换 else if((*(student *)a).score == (*(student *)b).score) if((*(student *)a).num > (*(student *)b).num) return 1; else return 0; else return -1; } int main() { student st[100]; int i,N; while(scanf("%d", &N) != EOF){ for(i=0; i<N; i++){ scanf("%d %d", &st[i].num, &st[i].score); } qsort(st, N,sizeof(st[0]), cmp); for(i=0; i<N; i++){ printf("%d %d\n", st[i].num,st[i].score); } } return 0; }
#include<iostream> #include<algorithm> #include<vector> using namespace std; struct student { unsigned id; unsigned score; }; int main() { int stdCount; cin >> stdCount; vector<student> vec; while (0 < stdCount--) { student s; cin >> s.id >> s.score; vec.push_back(s); } auto f = [](const student& s1, const student& s2)->bool {if (s1.score != s2.score)return s1.score < s2.score; return s1.id < s2.id; }; sort(vec.begin(), vec.end(), f); for (auto& e : vec) cout << e.id << " " << e.score << endl; return 0; }
#include<stdio.h> struct student { int id; int score; }; void swap(struct student *s,int j) { struct student t; t=s[j]; s[j]=s[j-1]; s[j-1]=t; return ; } int main() { int N; /* N个学生*/ int i,j; struct student s[100]; while(scanf("%d",&N)!=EOF) { //录入学生信息 for(i=0;i<N;i++) { scanf("%d %d",&s[i].id,&s[i].score); } //排序 for(i=0;i<N;i++) for(j=N-1;j>i;j--) { if(s[j].score<s[j-1].score) swap(s,j); else if(s[j].score==s[j-1].score) { if(s[j].id<s[j-1].id) swap(s,j); } } for(i=0;i<N;i++) { printf("%d %d\n",s[i].id,s[i].score); } } return 0; }
#include<iostream> #include<stdlib.h> using namespace std; typedef struct node{ int stu; int grade; }student; void sort(student s[],int N){ student t; for(int i = 0;i < N - 1;i++){ for(int j = 0;j < N - 1 - i;j++){ if(s[j + 1].grade < s[j].grade){ t = s[j + 1]; s[j + 1] = s[j]; s[j] = t; }else if(s[j + 1].grade == s[j].grade && s[j + 1].stu < s[j].stu){ t = s[j + 1]; s[j + 1] = s[j]; s[j] = t; } } } } int main(){ int N; cin>>N; student s[N]; for(int i = 0;i < N;i++){ cin>>s[i].stu; cin>>s[i].grade; } sort(s,N); for(int i = 0;i < N;i++){ cout<<s[i].stu<<" "<<s[i].grade<<endl; } return 0; }