Google赛马--chicy

题目:共有25匹马,有一个赛场,赛场有5个赛道,就是说最多同时可以有5匹马一起比赛。假设每匹马都跑的很稳定,不用任何其他工具,只通过马与马之间的比赛,试问最少得比多少场才能知道跑得最快的5匹马。

 

算法分析:

先分5组(a b c d e组)跑5
1场:a组的:比赛结果如下:a1, a2, a3, a4, a5
接下来四场分别是bcde组各自比赛,结果由名次由下标体现

 

6场:a2b2, c2, d2, e2
          
无论哪组胜出,该组的第一名肯定在跑得最快的五匹里max{ ××××× }
          
不妨假设a2胜出,则至此得出max{ a1×××× }

7场:a2b1c1d1, e1

         1)a2胜出,max{ a1a2××× },下一场a3b1c1d1, e1
         2)
不是a2胜出,max{ a1b1××× } 下一场  a2b2c1d1, e1

 

以后几场分别根据前次比赛结果,选获胜组中的次快马代替获胜马,进行与第7场同样的处理。

直到max里有5匹马。从第六场开始,每一场比赛都将选出一匹当前最快的马。

 

(举其中一种情况作为例子)

8a3b1c1d1, e1
处理方法跟第七场相同(该场后max至少有3个元素)

9a4b1c1d1, e1
处理方法跟第七场相同(该场后max至少有4个元素)

10a5b1c1d1, e1
处理方法跟第七场相同(该场后max至少有5个元素) 结束(共10场)

 

 

代码如下:

 

using System;

 

public class HorseRace

{

    private int[,] horses = new int[5,5];

    public HorseRace()

    {

        //initialize horses speeds with a Random instance.

        Random rd = new Random();

        for (int i = 0; i < 5; i++)

        {

            for (int j = 0; j < 5; j++)

            {

                horses[i, j] = rd.Next(1,60000);//suppose horse fastest speed is 60km/h

            }

        }

    }

 

    //sort each group's horses by their speeds

    public void ARace(int[,] k)

    {

        for (int l = 0; l < 5; l++)

        {

            for (int i = 0; i < 5; i++)

            {

                for (int j = i + 1; j < 5; j++)

                {

                    if (k[l, i] < k[l, j])

                    {

                        k[l, i] = k[l, i] + k[l, j];

                        k[l, j] = k[l, i] - k[l, j];

                        k[l, i] = k[l, i] - k[l, j];

                    }

                }

            }

        }

    }

 

    //find the fastest horse, then return the group number.

    public int Max(int[] k)

    {

        int max = 0;

        for (int i = 1; i < 5; i++)

        {

            if (k[i] > k[max])

            {

                max = i;

            }

        }

        return max;

    }

 

    //print the horses, one row one group.

    public void Print()

    {

        System.Console.WriteLine("=====================================");

        for (int i = 0; i < 5; i++)

        {

            System.Console.Write("|");

            for (int j = 0; j < 5; j++)

            {

                System.Console.Write("{0,5}==", horses[i, j]);

            }

            System.Console.Write("|");

            System.Console.WriteLine(" ");

        }

        System.Console.WriteLine("=====================================");

    }

 

    public void Fifth()

    {

        for (int i = 0; i < 5; i++)

        {

            ARace(horses);

        }

 

        int[] temp = new int[5];//store the speeds of horses who want to race next.

 

        //choose the second horse for race.

        for (int i = 0; i < 5; i++)

        {

            temp[i] = horses[i, 1];

        }

 

        int mm = Max(temp);//find the win group.

        int[] m = new int[5];//store the next horse(NO.) which want to race representive the current win group.

        m[mm] = 1;

        System.Console.Write("The dark horses are: {0,7}", horses[mm, 0]);

 

        for (int i = 0; i < 5; i++)

        {

            if (i != mm)

            {

                temp[i] = horses[i, 0];

            }

        }

 

        int count = 1;

        while (5 > count++)

        {

            mm = Max(temp);

            System.Console.Write("{0,7}", horses[mm, m[mm]++]);

            temp[mm] = horses[mm, m[mm]];

        }

    }

 

    public static void Main()

    {

        HorseRace h = new HorseRace();

        h.Print();

        h.Fifth();

    }

}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值