逖靖寒的世界

每天进步一点点

导航

TopCoder SRM 152 div 2 500point

这个题目比较简单,自己在纸上写出来给的例子,然后分析一下就会发现规律了:)



我的代码

#include<vector>
#include<iostream>
using namespace std;
 
class LeaguePicks 
{
public:
    vector<int> returnPicks(int position, int friends, int picks);
 
};
 
vector <int> LeaguePicks::returnPicks(int position, int friends, int picks)
{
    vector <int> receive_picks;
    int a[] = {(friends - position) * 2 + 1, (position - 1) * 2 + 1};
 
    if (picks >= position) {
        receive_picks.push_back(position);
        int index = position;
        int i = 0;
        while(true) {
            index += a[i];
            if (index <= picks) {
                if (i == 0) i = 1;
                else i = 0;
                receive_picks.push_back(index);
            }
            else break;
        }
    }
 
    return receive_picks;
}
 
void main()
{
    LeaguePicks test;
    vector <int> result = test.returnPicks(3, 6, 15);
    for(int i = 0; i < result.size(); i++) {
        cout<<result.at(i)<<" ";
    }
    cout<<endl;
    result = test.returnPicks(1, 1, 10);
    for(int i = 0; i < result.size(); i++) {
        cout<<result.at(i)<<" ";
    }
    cout<<endl;
    result = test.returnPicks(1, 2, 39);
    for(int i = 0; i < result.size(); i++) {
        cout<<result.at(i)<<" ";
    }
    cout<<endl;
    result = test.returnPicks(5, 11, 100);
    for(int i = 0; i < result.size(); i++) {
        cout<<result.at(i)<<" ";
    }
    cout<<endl;
}

posted on 2008-10-06 21:01  逖靖寒  阅读(593)  评论(0编辑  收藏  举报