题目:n支队伍比赛,分别编号为0,1,2。。。。n-1,已知它们之间的实力对比关系,
存储在一个二维数组w[n][n]中,w[i][j] 的值代表编号为i,j的队伍中更强的一支。
所以w[i][j]=i 或者j,现在给出它们的出场顺序,并存储在数组order[n]中,
比如order[n] = {4,3,5,8,1......},那么第一轮比赛就是 4对3, 5对8。.......
胜者晋级,败者淘汰,同一轮淘汰的所有队伍排名不再细分,即可以随便排,
下一轮由上一轮的胜者按照顺序,再依次两两比,比如可能是4对5,直至出现第一名
编程实现,给出二维数组w,一维数组order 和 用于输出比赛名次的数组result[n],求出result。
思路:此题看似很复杂,其实应该就是一轮轮的判断,直到队伍剩下1支。
void RaceResult(int** w, int* order, int* result, int n)
{
list<int> winer;
int count = n;
while(n)
winer.push_front(order[--n]);
int resultNum = count - 1;
int nFirst, nSecond;
int round = 1;
while(winer.size() > 1)
{
//一轮开始
cout<<"round "<<round++<<endl;
list<int>::iterator it = winer.begin();
while (it != winer.end())
{
nFirst = *it;
if (++it == winer.end())//轮空
cout<<nFirst<<" rest this round"<<endl;
else
{
nSecond = *it;
int nWiner = *((int*)w + count * nFirst + nSecond);
if (nWiner == nFirst)
{
it = winer.erase(it);
result[resultNum--] = nSecond;
cout<<nFirst<<" kick out "<<nSecond<<endl;
}
else
{
it = winer.erase(--it);
result[resultNum--] = nFirst;
++it;
cout<<nSecond<<" kick out "<<nFirst<<endl;
}
}
}
}
if (winer.size() == 1)
result[0] = winer.front();
cout<<endl<<"final result: ";
int nPlace = 0;
while(nPlace < count)
cout<<result[nPlace++]<<" ";
cout<<endl;
}
void test()
{
//team 2>team 1>team 3>team 0>team 4>team 5
int w[6][6] = {
0,1,2,3,0,0,
1,1,2,1,1,1,
2,2,2,2,2,2,
3,1,2,3,3,3,
0,1,2,3,4,5
};
int order[6] = {1,3,4,2,0,5};
int result[6] = {-1};
RaceResult((int**)w, order, result, 6);
}