版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37036984/article/details/79655446
题目:查找和排序
**题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
都按先录入排列在前的规则处理。**
例示:
jack 70
peter 96
Tom 70
smith 67
从高到低 成绩
peter 96
jack 70
Tom 70
smith 67
从低到高
smith 67
Tom 70
jack 70
peter 96
输入描述:
输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开
输出描述:
按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开
示例1
输入
3
0
fang 90
yang 50
ning 70
输出
fang 90
ning 70
实现代码:
#include <iostream>
using namespace std;
struct Student{
string name;
int score;
};
int main()
{
int num,t;
while(cin>>num>>t){
Student stu[num];
for(int i = 0;i<num; i++){
cin>>stu[i].name>>stu[i].score;
}
if(t == 0){//按照降序排列
for(int i = 0;i<num;i++){
for(int j = 0;j<num-i-1;j++){
if(stu[j].score<stu[j+1].score){
Student temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}else if(t == 1){//按照升序排列
for(int i = 0;i<num;i++){
for(int j = 0;j<num-i-1;j++){
if(stu[j].score>stu[j+1].score){
Student temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}else{
return 0;
}
for(int i = 0;i<num; i++){
cout<<stu[i].name<<" "<<stu[i].score<<endl;
}
}
return 0;
}