Question 1196: Sorting of scores, 1196: Sorting of scores
-
Description:
-
Use a one-dimensional array to store student IDs and scores, and then sort and output results by score.
-
Input:
-
The first line contains an integer N (1 <=n <= 100), representing the number of students.
Each row in the next N rows contains two integers, p and q, representing the student's student ID and score respectively.
-
Output:
-
Sort the scores of students from small to large, and print the sorted student information.
If the scores of students are the same, the student IDs are sorted in ascending order.
-
Sample input:
-
31 902 873 92
-
Sample output:
-
2 871 903 92
C ++ code:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; const int maxn=100+3; int num[maxn],grade[maxn]; int main() { int n; while(scanf("%d",&n)!=EOF&&(n>=1&&n<=100)) { for(int a=0;a<n;a++) scanf("%d%d",&num[a],&grade[a]); for(int i=n-1;i>=1;i--) for(int j=0;j<i;j++) if(grade[j]<grade[j+1]||(grade[j]==grade[j+1])&&(num[j]<num[j+1])) { int temp=grade[j]; grade[j]=grade[j+1]; grade[j+1]=temp; temp=num[j]; num[j]=num[j+1]; num[j+1]=temp; } for(int b=n-1;b>=0;b--) { printf("%d %d\n",num[b],grade[b]); } } return 0; }/************************************************************** Problem: 1196 User: Carvin Language: C++ Result: Accepted Time:50 ms Memory:1520 kb****************************************************************/
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.