北郵OJ不讓上了铐懊,可能快復(fù)試的原因
排序用 冒泡 一次定一個(gè)數(shù)的位置
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){? ?//循環(huán)判斷條件
? ? ? ? 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;
}
題目描述
查找和排序
題目:輸入任意(用戶祟蚀,成績(jī))序列,可以獲得成績(jī)從高到低或從低到高的排列,相同成績(jī)
都按先錄入排列在前的規(guī)則處理割卖。
示例:
jack??????70
peter?????96
Tom???????70
smith?????67
從高到低??成績(jī)?
peter?????96?
jack??????70?
Tom???????70?
smith?????67
從低到高
smith?????67
jack? ? ? 70?
Tom? ? ? 70?
peter?????96
輸入描述:
輸入多行前酿,先輸入要排序的人的個(gè)數(shù),然后輸入排序方法0(降序)或者1(升序)再分別輸入他們的名字和成績(jī)鹏溯,以一個(gè)空格隔開
輸出描述:
按照指定方式輸出名字和成績(jī)罢维,名字和成績(jī)之間以一個(gè)空格隔開
示例1
輸入
3
0
fang 90
yang 50
ning 70
輸出
fang 90
ning 70
yang 50