2012-10-22 20 views
0

我有一個16的數組(下面的例子)。比賽的每場比賽都包括顯示兩家餐廳,提示選擇他最喜歡的餐廳,並從比賽中移除丟失的餐廳。 它就像一個支架系統;即,在所有其他餐館也出現在該輪的比賽中之前餐廳不會再出現在另一場比賽中)。因此,從8場比賽開始,然後是4場,然後是2場。 除非餐館數量等於16,否則不要讓錦標賽開始。 我對C++很新穎。任何人都有一個體面的大綱或建議/路徑我應該看看?我將如何實施這樣的比賽? C++

string restaurants[] = {"Texas Roadhouse,","On The Border,","Olive Garden,","Panda Express,","Cracker Barrel,","IHOP,","Panda Express,","Pei Wei"}; 
+0

這似乎是功課。是嗎?作爲第一步,我建議提出一種用僞碼編寫的算法來解決這個問題。 – Alan

回答

0

我只是要寫出代碼的要點,請進一步探討如何實現它。

//assume arr is the array of 16 and length =16 initially 
    while(length >0) 
    { 
    for(i=0;i<length; i=i+2) 
    { 
     int first= rand % (16-i); 

     int second=rand % (16-i); 
     while(first==second) 
     second=rand%16; 

     int chosen=choose_restaurant(arr,first,second,length); 
     //return either first or second, depending on what is chosen 

     if(chosen ==first) 
     remove_restaurant(arr,second,length); 
     else 
     remove_restaurant(arr,first,length); 

    } 
    length=length/2; 
    } 

//the remove_restaurant function must move the removed restaurant from the array 
//And move the selected restaurant to the end