Problem description
There are 36 horses, to run on the runway with 6 lanes, run up to 6 at a time, without a timer, and ask at least how many times you need to run to get the fastest three horses?
C + + implementation
#include <ctime>#include<cstdlib>#include<iostream>#include<map>using namespacestd;typedef Map<int,int*>Intpintmap;//Bubble SortvoidBubblesort (intArr[],intLen) { intI, J; for(i =0; I < Len-1; i++) { for(j =0; J < Len-i-1; J + +) { if(Arr[j]>arr[j +1]) { inttemp =Arr[j]; ARR[J]= Arr[j +1]; Arr[j+1] =temp; } } }}voidFunintarr[]) { inti; inttemp[6]; Intpintmap Coll; Intpintmap::iterator POS; //6*6 first race one at a time for(i=0;i<6;++i) bubblesort (arr+i*6,6); /*match the first of the 6 games above (map will automatically sort) and put its corresponding address in map*/ for(i=0;i<6;++i) Coll.insert (Make_pair (arr[i*6],arr+i*6)); /*Put the first one, start the top three in the temp array will this second, start the first two into the temp array in the first place in the third, the beginning of the previous put into the temp array*/ for(Pos=coll.begin (), i=0;p os!=coll.end (); ++pos,++i) {if(0==i) {temp[0]=pos->second[0]; temp[1]=pos->second[1]; temp[2]=pos->second[2]; } if(1==i) {temp[3]=pos->second[0]; temp[4]=pos->second[1]; } if(2==i) temp[5]=pos->second[0]; } //sort the temp array and print out the top threeBubblesort (temp,6); printf ("%d%d%d\n", temp[0],temp[1],temp[2]); }intMain () {intarr[ $]; inti; for(i=0;i< $;++i) {Arr[i]=rand ()% +; printf ("%d\t", Arr[i]); } printf ("\ n"); Fun (arr);}
36 Horses, 6 runway problems (C + + implementation)