成績排序 ??途W(wǎng)

北郵OJ不讓上了,可能快復(fù)試的原因

排序用 冒泡 一次定一個數(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;

}


題目描述

查找和排序

題目:輸入任意(用戶,成績)序列,可以獲得成績從高到低或從低到高的排列,相同成績

都按先錄入排列在前的規(guī)則處理。

示例:

jack??????70

peter?????96

Tom???????70

smith?????67

從高到低??成績?

peter?????96?

jack??????70?

Tom???????70?

smith?????67

從低到高

smith?????67

jack? ? ? 70?

Tom? ? ? 70?

peter?????96

輸入描述:

輸入多行,先輸入要排序的人的個數(shù),然后輸入排序方法0(降序)或者1(升序)再分別輸入他們的名字和成績,以一個空格隔開

輸出描述:

按照指定方式輸出名字和成績,名字和成績之間以一個空格隔開

示例1

輸入

復(fù)制

3

0

fang 90

yang 50

ning 70

輸出

復(fù)制

fang 90

ning 70

yang 50

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容