五十八.L1-030 一帮一

在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
struct node
{
    
    
    int a;
    string s;
};
int main()
{
    
    
    int n;
    cin >> n;
    vector<node>stu;
    for(int i = 0; i<n; i++)
    {
    
    
        node temp;
        cin >> temp.a >> temp.s;
        stu.push_back(temp);
    }
    int j = n - 1;
    int k = n - 1;
    for(int i = 0; i<n/2; i++)
    {
    
    
        if(stu[i].a==1)
        {
    
    
            while(stu[j].a==stu[i].a)
            {
    
    
                j--;
            }
            cout << stu[i].s << " " << stu[j].s << endl;
            j--;
        }
        else
        {
    
    
            while(stu[k].a==stu[i].a)
            {
    
    
                k--;
            }
            cout << stu[i].s << " " << stu[k].s << endl;
            k--;
        }
    }
    return 0;
}

在这里插入图片描述
先把数据存入结构体数组中,用vector()函数实现。用两个变量代表男女,从末位开始遍历,找到没被使用过的异性,进行配对。

猜你喜欢

转载自blog.csdn.net/JiangYu200015/article/details/108720226