PAT 1109(超简单双向队列解法)

PAT 1109

双向队列很简单,就多一个前加后加和前删后删的区别

直接上代码:

#include <cstdio>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;

int N,K,cnt;
struct person{
    string name;
    int height;
};

bool cmp(person a,person b){
    if(a.height != b.height)
        return a.height > b.height;
    else
        return a.name < b.name;
}

//13:43  14:28
int main(){
    cin >> N >> K;
    cnt=0;//用于统计当前输出到第几个元素
    
    //获得学生数据数组stu
    person stu[N];
    for(int i=0;i<N;i++)
        cin >> stu[i].name >> stu[i].height;
    sort(stu,stu+N,cmp);
    
    //声明一个矩阵数组存放摆放的位置
    int arr = N/K+N-N/K*K;
    
    //开始将名字填入矩阵
    for(int i=0;i<K;i++){
        int size;//代表本次要处理的元素
        deque<string> tmp;//用于处理排序的双向队列
        bool flag = true;//判断前加还是后加
        if(i==0) size = arr;
            else size = N/K;
        for(int j=0;j<size;j++){
            if(!flag)//前面
                tmp.push_front(stu[cnt++].name);
            else {//后面
                tmp.push_back(stu[cnt++].name);
            }
            flag = !flag;
        }
        
        while(!tmp.empty()){
            cout << tmp.front();
            if(tmp.size()!=1) cout << " ";
            tmp.pop_front();
        }
        cout << endl;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值