PAT---1056 Mice and Rice(队列,模拟)

题意:输入一组老鼠的体重,要求按照每组ng个人进行分组,选出每一组体重最大的老鼠进入下一轮,依次重复,直到选出体重最大的老鼠。
参考:https://blog.csdn.net/qq_34649947/article/details/79532582

#include <iostream>
#include <queue>

using namespace std;

struct Node
{
    int weight, ranks;
};

Node list1[1001];

int main()
{
    int n, g;
    cin >> n >> g;
    for(int i = 0; i < n; i++)
        cin >> list1[i].weight;

    queue<int> order;
    int k;
    for(int i = 0; i < n; i++)
    {
        cin >> k;
        order.push(k);
    }

    int num = n;    //表示每一轮的总人数
    int groups;      //表示有几组
    while(order.size() != 1)
    {
        if(num % g)  groups = num/g+1;
        else  groups = num/g;

        for(int i = 0; i < groups; i++)
        {
            int maxw = order.front();   //记录每一组的最大值id
            for(int j = 0; j < g; j++)
            {
                //如果编号超过这一轮的总人数
                if(i*g + j >= num)  break;
                if(list1[maxw].weight < list1[order.front()].weight)
                    maxw = order.front();

                list1[order.front()].ranks = groups + 1;
                order.pop();   //出队
            }
            //将每一组的最大值id入队
            order.push(maxw);
        }
        num = groups;
    }
    list1[order.front()].ranks = 1;
    
    for(int i =0; i < n; i++)
        if(i == n-1)
            cout << list1[i].ranks << endl;
        else
            cout << list1[i].ranks << " ";
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值