分为6组,定为a,b,c,d,e,f组,每组6匹马
第一轮,每组跑一次,取其冠军,假设每组的冠军是a1,b1,c1,d1,e1,f1
第二轮,将每组的冠军放一组里跑一次,取前3,假设为a1,b1,c1
(a1已为总冠军,无需再跑;c1是第3名,所以c1组其他成员无需再跑;d,e,f组所有成员也无需再跑)
第三轮,取a2, a3, b1, b2, b3, c1放一组里跑一次,取前2名,再加a1,即是最快的三匹马
总跑的次数:6+1+1=8
void HorseRace()
{
int horses[36]{};
for (int i = 0; i < 36; i++)
{
horses[i] = rand(); //随机马匹
}
//第一轮:分为6组,进行6次比赛,horses[i*6]是每组的冠军
for (int i = 0; i < 6; i++)
{
std::sort(&horses[i * 6], &horses[i * 6 + 6], [](auto a, auto b) {return a > b; });
}
int first_champion[6];
for (int i = 0; i < 6; i++)
{
first_champion[i] = horses[i * 6]; //取每组的冠军
}
//第二轮:将每组的冠军比赛1次
std::sort(&first_champion[0], &first_champion[6], [](auto a, auto b) {return a > b; });
int final_champion[3]; //前3名的成绩
final_champion[0] = first_champion[0]; //总冠军,已经产生
int second_champion[6];
for (int i = 0; i < 6; i++)
{
if (first_champion[0] == horses[i * 6]) //找a1组
{
//找到a2与a3
second_champion[0] = horses[i * 6 + 1];
second_champion[1] = horses[i * 6 + 2];
}
else if (first_champion[1] == horses[i * 6]) //找b1组
{
//找到b1 b2 b3
second_champion[2] = horses[i * 6 + 0];
second_champion[3] = horses[i * 6 + 1];
second_champion[4] = horses[i * 6 + 2];
}
}
second_champion[5] = first_champion[2]; //c1再次参与比赛
//第三轮:比赛1次
std::sort(&second_champion[0], &second_champion[6], [](auto a, auto b) {return a > b; });
//取前2名
final_champion[1] = second_champion[0];
final_champion[2] = second_champion[1];
//总成绩的前3名
for (int i = 0; i < 3; i++)
{
printf("%d ", final_champion[i]);
}
}