北邮OJ不让上了,可能快复试的原因
排序用 冒泡 一次定一个数的位置
i=0;i<n;i++
j=n-1;j>i;j--;
#include<stdio.h>
#include<string>
#include<iostream>
#define MAX 10000
using namespace std;
struct student{
string name;
int score;
};
int main(){
int t,n;
struct student stu[MAX]; //定义方式
while( scanf("%d%d",&t,&n)!=EOF){ //循环判断条件
for(int i=0;i<t;i++){
cin>>stu[i].name>>stu[i].score;
}
if(n==0){
for(int i=0;i<t;i++){
for(int j=t-1;j>i;j--){
if(stu[j].score>stu[j-1].score){
struct student temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
}
}
}
}
else {
for(int i=0;i<t;i++){
for(int j=t-1;j>i;j--){
if(stu[j].score<stu[j-1].score){
student temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
}
}
}
}
for(int i=0;i<t;i++){
cout<<stu[i].name<<' '<<stu[i].score;
cout<<endl;
}
}
return 0;
}
题目描述
查找和排序
题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
都按先录入排列在前的规则处理。
示例:
jack 70
peter 96
Tom 70
smith 67
从高到低 成绩
peter 96
jack 70
Tom 70
smith 67
从低到高
smith 67
jack 70
Tom 70
peter 96
输入描述:
输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开
输出描述:
按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开
示例1
输入
3
0
fang 90
yang 50
ning 70
输出
fang 90
ning 70
yang 50