topcoder题目及我的程序(3)——league picks

本文介绍了一种用于TopCoder联赛中公平分配选手选择权的算法。该算法根据玩家在联赛中的位置,采用循环反转的方式分配选手的选择顺序,并提供了一个C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一道topcoder题目,比较简单

==============================================================

You and your friends are setting up a fantasy TopCoder league, where you choose coders to be on your team and score points in the league when any one of your coders wins their room or successfully challenges somebody, etc. To be fair, a system has been developed to choose the order in which picks are distributed. It works like this: first, lots are drawn to choose your position in the league. Then the player with the first position gets first pick, the second player gets second pick, all the way until the last player picks. Then the order reverses: the last player chooses again, then the next to last player, and so on, until you reach the first player again. Then the cycle repeats: the first position chooses again, then the second, and so on.

For example: say you were in the third position on a 6 player league. You would get the 3rd pick, then you'd wait until the 10th pick (the order would be 1,2,you,4,5,6,6,5,4,you), and then the 15th pick, and so on until there were no more coders to choose. If there were 20 total picks, then you would get pick numbers 3,10,15.

Not wanting to miss your chance at a pick, your goal is to write a program that tells you what pick numbers you have in the order that you have them. You will receive three ints indicating your position in the league(1 being the first position), the number of friends that are in the league with you, and the number of picks that are being divvied up among the league. You will return an int[] that indicates the picks that you receive in ascending order.

Definition
????
Class:
LeaguePicks
Method:
returnPicks
Parameters:
int, int, int
Returns:
int[]
Method signature:
int[] returnPicks(int position, int friends, int picks)
(be sure your method is public)
????

Notes
-Note that your position in the league and the pick numbers start at 1 and not 0. This should be clear from the examples. Constraints
-position will be between 1 and friends inclusive.
-friends will be between 1 and 40 inclusive.
-picks will be between 1 and 40 * friends inclusive.

Examples
0)

????
3
6
15
Returns: { 3,  10,  15 }
Example from above.
1)

????
1
1
10
Returns: { 1,  2,  3,  4,  5,  6,  7,  8,  9,  10 }
You're the only player, so you get all the picks.
2)

????
1
2
39
Returns:
{ 1,  4,  5,  8,  9,  12,  13,  16,  17,  20,  21,  24,  25,  28,  29,
  32,  33,  36,  37 }
You'll get the 1st and 4th picks in every set of 4.
3)

????
5
11
100
Returns: { 5,  18,  27,  40,  49,  62,  71,  84,  93 }
You'll get the 5th and (2*11-5+1) or 18th picks out of every 2*11 picks.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
==============================================================

源程序如下

#include<stdio.h>
#include
<vector>

#define MAXPICKS 40

using namespace std;

class LeaguePicks
{
public:
    
int m_position;    //your position
    int m_friends;    //the number of all friends
    int m_picks;    //the number of all picks

    vector 
<int> m_vecPicknumbers;

public:
    LeaguePicks();
    
~LeaguePicks();

    
//it can also return the type of "vector<int>"
    void returnPicks(int position,int friends,int picks);

    
void display();
}
;

LeaguePicks::LeaguePicks()
{    
}


LeaguePicks::
~LeaguePicks()
{
}


void LeaguePicks::returnPicks(int position, int friends, int picks)
{
    
int i=0,j=0,s=0;
    
int pick1,pick2;

    m_vecPicknumbers.clear();
    
while(1)        
    
{
        pick1
=s+position;
        pick2
=s+2*friends-position+1;

        
if(pick1>picks)
            
break;
        m_vecPicknumbers.push_back(pick1);

        
if(pick2>picks)
            
break;
        m_vecPicknumbers.push_back(pick2);
        
        s
+=2*friends;
    }

}


//display your pick numbers
void LeaguePicks::display()
{
    
for(int i=0;i<m_vecPicknumbers.size();i++)
        printf(
"%3d",m_vecPicknumbers.at(i));
    printf(
" ");
}


void main()
{
    LeaguePicks pick0,pick1,pick2,pick3;

    
//case 0
    pick0.m_position=3;
    pick0.m_friends
=6;
    pick0.m_picks
=15;

    
//case 1
    pick1.m_position=1;
    pick1.m_friends
=1;
    pick1.m_picks
=10;

    
//case 2
    pick2.m_position=1;
    pick2.m_friends
=2;
    pick2.m_picks
=39;

    
//case 3
    pick3.m_position=5;
    pick3.m_friends
=11;
    pick3.m_picks
=100;

    pick0.returnPicks(pick0.m_position,pick0.m_friends,pick0.m_picks);
    pick1.returnPicks(pick1.m_position,pick1.m_friends,pick1.m_picks);
    pick2.returnPicks(pick2.m_position,pick2.m_friends,pick2.m_picks);
    pick3.returnPicks(pick3.m_position,pick3.m_friends,pick3.m_picks);

    pick0.display();
    pick1.display();
    pick2.display();
    pick3.display();
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值