当前位置:网站首页>Pta:6-33 student ranking table (destructor)

Pta:6-33 student ranking table (destructor)

2022-06-23 04:37:00 Sy_ Faker

Now enter a group of students ( There are more people than 0 And no more than 100) Their ranking and their names . It is required to output everyone's ranking by ranking .

Input format : The information of each student , Two items in total , The first item is the ranking ( As a positive integer , And the ranking of any two students is different ), The second item is the student's name . When the input -1 when , End of input .

Output format : Output student names by rank , Each row of a .

Function interface definition :
main Part of the function .

Sample referee test procedure :

#include <iostream>
#include <string>
using namespace std;
class Student{
    
    int rank;
    string name;
    public:
        int getRank(){
    return rank;	}
        Student(string name, int rank):name(name), rank(rank){
    	}
        ~Student(){
     cout<<name<<endl;}
};
int main(){
    
    int rank, count=0;
    const int SIZE=100;
    Student *pS[SIZE];
    string name;
    cin>>rank;
    while(count<SIZE && rank>0){
    
        cin>>name;
        pS[count++]= new Student(name, rank);
        cin>>rank;
    }

/*  Please fill in the answer here  */

    return 0;
}

sample input :
1 Jack
5 Peter
2 Alice
6 Kate
52 Mike
-1

sample output :
Jack
Alice
Peter
Kate
Mike

I made it with bubble :

	Student *temp;
	for(int i=0;i<count;i++)
	{
    
		for(int j=i+1;j<count;j++)
		{
    
			if(pS[i]->getRank()>pS[j]->getRank())
			{
    
				temp=pS[i];
				pS[i]=pS[j];
				pS[j]=temp;
			}
		}
	}
	for(int i=0;i<count;i++)
		delete pS[i];
原网站

版权声明
本文为[Sy_ Faker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222259539247.html