Question 1196: Sorting of scores, 1196: Sorting of scores

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.