Problem C: Score sort time limit:1 Sec Memory limit:128 MB
submit:2535 solved:1988
[Submit] [Status] [Web Board] Description
Define the Student class:
1. Data members string name and int score represent a student's name and score.
2. No parameter constructor.
3. void Setstudent (String,int) method, used to set a student's attribute value.
4. Overloaded > (greater than Operator). The size relationship of objects A and B of the student class, a>b defined as A.score>b.score, or A.score=b.score but a.name<b.name.
5. Overloaded operator << for output student information: first output the result and then output the name, the middle with a space separated.
Input
Divided into multiple lines. The first m>0 indicates that there is a message for m students.
Then there is M-line, each line is a student information. The first part is the student's name, the second part is the student achievement.
Output
The output is M-line, outputting each student's score and name in order from large to small. Assume that there are no students with duplicate names.
Sample Input
5Tom 98Jack 97Mary 98Sherry 99Dock 97
Sample Output
Sherry98 Mary98 Tom97 Dock97 Jack
HINT
The string class has a method: Int compare (const string &s) const, which is used to compare the size of the current string and s to the same principle as the C-language library function strcmp.
Append codeappend.cc,
int main () { int cases; string name; int score; cin>>cases; Student students[cases], temp; for (int i = 0; i < cases; i++) { cin>>name>>score; Students[i].setstudent (name, score); } for (int i = 0; i < cases; i++.) {for (int j = 0; J < cases-i-1, j + +) { if (!) ( STUDENTS[J] > students[j + 1]) { temp = students[j]; STUDENTS[J] = students[j + 1]; Students[j + 1] = temp;}} } for (int i = 0; i < cases; i++) cout<<students[i]<<endl; return 0;}
#include <iostream>using namespace Std;class student{public:string name; int score; Student (): Name ("No Name"), score (0) {} void Setstudent (String name_,int score_) {name= name_; score= score_; } bool Operator> (Student &p) {if (Score>p.score) return true; else if (score==p.score&&name<p.name) return true; else return false; } friend Ostream &operator<< (ostream &os, Student &p1) {os<<p1.score<< "" <& Lt;p1.name; return OS; }};int Main () {int cases; String name; int score; cin>>cases; Student students[cases], temp; for (int i = 0; i < cases; i++) {cin>>name>>score; Students[i].setstudent (name, score); } for (int i = 0; i < cases; i++.) {for (int j = 0; J < cases-i-1, j + +) {if (!) ( STUDENTS[J] > students[j + 1])) {temp = Students[j]; STUDENTS[J] = students[j + 1]; Students[j + 1] = temp; }}} for (int i = 0; i < cases; i++) cout<<students[i]<<endl; return 0;}
Problem C: Ranking of grades