一个facebook面试题的算法求解

转自我的QQ空间

  1 今天上午在实验室的时候无意间看到了世界五百强的面试题,被facebook的一道题所吸引,所以就总结了一下思路。把程序写了出来。
  2 刚才写完测试了一下,基本没什么问题,而起在完成后发现有些步骤其实就是多余的~~~
  3 题目如下:
  4 Facebook:25匹赛马,没有秒表,五条跑道。用最少的比赛场次找出三匹跑的最快的马。
  5 我在完成程序后得到的结果就是只要7次就能得到最快的3匹马。
  6 代码如下,没用的地方请自行忽略。
  7  #include <iostream>
  8 #include <ctime>
  9 #include <cstdlib>
 10 #define x 5
 11 #define fast3 3
 12 #define ExChange(a,b) {(a)+=(b);(b)=(a)-(b);(a)=(a)-(b);}
 13 using namespace std;
 14 class FHorse
 15 {
 16     private:
 17             struct HorseInfo
 18             {
 19                 int speed;
 20                 int team;
 21             };
 22             int horse[x][x];
 23             HorseInfo FastHorseList[x];
 24             HorseInfo Top3[fast3];
 25             int Comparetime;
 26             void initial();
 27             void FHinitial();
 28             int iComptime();
 29             int Maxsort(int * array,int length);
 30             void FHMaxSort();
 31     public:
 32             FHorse();
 33             ~FHorse();
 34             int Test(int times=0);
 35             void Dispaly();
 36 };
 37 FHorse::FHorse()
 38 {
 39     initial();
 40     iComptime();
 41 }
 42 FHorse::~FHorse()
 43 {
 44     ;//Nothing to add
 45 }
 46 void FHorse::initial()
 47 {
 48     Comparetime = 0;
 49     srand(time(NULL));
 50     for(int i = 0;i < x;i++)
 51     for(int j = 0;j < x;j++)
 52         horse[i][j] = rand() % 25;
 53     //Dispaly();
 54 }
 55 int FHorse::Test(int times)
 56 {
 57     int totaltime = times;
 58     int SumCompareTime = 0;
 59     while(times--)
 60     {
 61         SumCompareTime += iComptime();
 62         if(times == 0)
 63             break;
 64         initial();
 65     }
 66 }
 67 int FHorse::iComptime()
 68 {
 69     int cmptime = 0;
 70     for(int i = 0;i < x;i++)//相当于最初的五场比赛
 71         Maxsort(horse[i],x);
 72     cmptime += x;
 73     FHinitial();//No problem
 74     cmptime +=1;
 75     //这也是一次比较
 76     Top3[0].speed = FastHorseList[0].speed;
 77     Top3[0].team  = FastHorseList[0].team;
 78     FastHorseList[0].speed = horse[FastHorseList[0].team][1];
 79     FastHorseList[3].speed = horse[FastHorseList[0].team][2];
 80     FastHorseList[3].team  = FastHorseList[0].team;
 81     FastHorseList[4].speed = horse[FastHorseList[1].team][1];
 82     FastHorseList[4].team = FastHorseList[1].team;
 83     FHMaxSort();
 84     cmptime +=1;
 85     Top3[1] = FastHorseList[0];
 86     Top3[2] = FastHorseList[1];
 87     return cmptime;
 88 }
 89 int FHorse::Maxsort(int * array,int length)
 90 {
 91     for(int i = 0;i < length - 1;i++)
 92     for(int j = 0;j < length - i - 1;j++)
 93     {
 94         if(array[j] < array[j+1])
 95             ExChange(array[j],array[j+1]);
 96     }
 97 }
 98 void FHorse::FHMaxSort()
 99 {
100     int tempspeed;
101     int tempteam;
102     for(int i = 0;i < x - 1;i++)
103     for(int j = 0;j < x - i -1;j++)
104     {
105         if(FastHorseList[j].speed < FastHorseList[j + 1].speed)
106         {
107             tempspeed = FastHorseList[j].speed;
108             tempteam  = FastHorseList[j].team;
109             FastHorseList[j].speed = FastHorseList[j + 1].speed;
110             FastHorseList[j].team  = FastHorseList[j + 1].team;
111             FastHorseList[j + 1].speed = tempspeed;
112             FastHorseList[j + 1].team  = tempteam;
113         }
114     }
115 }
116 void FHorse::FHinitial()
117 {
118     for(int i = 0;i < x;i++)
119     {
120         FastHorseList[i].speed = horse[i][0];
121         FastHorseList[i].team  = i;
122     }
123     FHMaxSort();
124 }
125 void FHorse::Dispaly()
126 {
127     for(int i = 0;i < x;i++)
128     for(int j = 0;j < x;j++)
129     {
130         cout<<horse[i][j]<<ends;
131         if(j == x - 1)
132             cout<<endl;
133     }
134     for(int i = 0;i < x;i++)
135     {
136         cout << "Horse Speed : " << FastHorseList[i].speed<<endl;
137         cout << "Horse Team :  "<< FastHorseList[i].team<<endl;
138     }
139     cout<<"The Top 3 Horse\n";
140     for(int i = 0;i < fast3;i++)
141     {
142         cout << "Horse Speed : " << Top3[i].speed<<endl;
143         cout << "Horse Team :  "<< Top3[i].team<<endl;
144     }
145 }
146 int main()
147 {
148     cout << "Hello world!" << endl;
149     FHorse temp;
150     temp.Dispaly();
151 //    int a = 2, b = -1;
152 //    ExChange(a,b);
153 //    cout<<a<<ends<<b<<endl;
154     return 0;
155 }
156 因为在写代码前思路不是很清晰,误以为每次的比赛次数不一定,所以写了一个test去求均值。
157 但是现在看完全不用了。
158 具体的算法我就懒得打字了,其中的一些情况在我的多次测试下基本正确。
159 如果算法有问题,可以问我,语法问题概不负责。

 

posted @ 2013-03-29 09:24  Matrix_R  阅读(1106)  评论(0编辑  收藏  举报