L1-030 一幫一 (15分)

題目描述:

“一幫一學習小組”是中小學中常見的學習組織方式,老師把學習成績靠前的學生跟學習成績靠後的學生排在一組。本題就請你編寫程序幫助老師自動完成這個分配工作,即在得到全班學生的排名後,在當前尚未分組的學生中,將名次最靠前的學生與名次最靠後的異性學生分爲一組。

輸入格式:

輸入第一行給出正偶數N(≤50),即全班學生的人數。此後N行,按照名次從高到低的順序給出每個學生的性別(0代表女生,1代表男生)和姓名(不超過8個英文字母的非空字符串),其間以1個空格分隔。這裏保證本班男女比例是1:1,並且沒有並列名次。

輸出格式:

每行輸出一組兩個學生的姓名,其間以1個空格分隔。名次高的學生在前,名次低的學生在後。小組的輸出順序按照前面學生的名次從高到低排列。

輸入樣例:

8
0 Amy
1 Tom
1 Bill
0 Cindy
0 Maya
1 John
1 Jack
0 Linda

輸出樣例:

Amy Jack
Tom Linda
Bill Maya
Cindy John
#include <iostream>
#include <string>
#include <stack>

using namespace std;

struct {
    int sex;
    string name;
}stu[25];

stack<string> boy, girl;

int main()
{
    int n, m;
    int sex;
    string name;
    
    cin >>  n;
    m = n / 2;
    
    for (int i = 0; i < m; i++) // 前n/2個入隊
        cin >> stu[i].sex >> stu[i].name;
    for (int i = 0; i < m; i++){ // 後n/2個分性別入棧
        cin >> sex >> name;
        if (sex == 1)
            boy.push(name);
        else
            girl.push(name);
    }
    
    for (int i = 0; i < m; i++){
        cout << stu[i].name << " ";
        if (stu[i].sex == 1)
            cout << girl.top() << endl, girl.pop();
        else
            cout << boy.top() << endl, boy.pop();
    }
    
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章