2012-01-26 79 views
2

對於我的計算機編程課程,我決定製作一個程序,生成一個隨機硬幣翻轉並記錄頭部和尾部最長的連續條紋。我搜查了互聯網,發現沒有答案。計數顯示不正確。即使只是一個提示將是偉大的! 謝謝, 賈斯汀C++保持最長的連續記錄

int main(){ 

    int number_of_flips; 
    int coin_flip; 
    int previous_flip = 2; 
    int head_count = 0; 
    int tail_count = 0; 
    int highest_head = 0; 
    int highest_tail = 0; 

    srand(time(NULL)); 

    cout << "Enter the number of coin flips:" << endl; 
    cin >> number_of_flips; 
    system("cls"); 

    for(int i = 0; i < number_of_flips; i++){ 


     coin_flip = rand() % 2; 

     if(coin_flip == 0){ 
      cout << "Heads" << endl; 
      if(coin_flip == previous_flip){ 
        head_count = head_count + 1;  
      } 
      else{ 
       if(head_count > highest_head){ 
        highest_head = head_count;     
       } 

       head_count = 0;  
      } 
     } 

     if(coin_flip == 1){ 
      cout << "Tails" << endl; 
      if(coin_flip == previous_flip){ 
        tail_count = tail_count + 1;   
      } 
      else{ 
       if(tail_count > highest_tail){ 
         highest_tail = tail_count;    
       } 

       tail_count = 0;  
      } 
     } 


     previous_flip = coin_flip; 
    } 

    cout << "The longest run of heads is " << highest_head << endl; 
    cout << "The longest run of tails is " << highest_tail << endl; 

    system("pause"); 
    return 0; 
} 

下面是輸出的一個例子:

Tails 
Tails 
Tails 
Heads 
Heads 
Tails 
Tails 
Tails 
Tails 
Heads 
The longest run of heads is 1 
The longest run of tails is 2 

作爲參考,這是我最後的代碼,我認爲現在的工作:

int main(){ 

    int number_of_flips; 
    int coin_flip; 
    int previous_flip = 2; 
    int head_count = 0; 
    int tail_count = 0; 
    int highest_head = 0; 
    int highest_tail = 0; 

    srand(time(NULL)); 

    cout << "Enter the number of coin flips:" << endl; 
    cin >> number_of_flips; 
    system("cls"); 

    for(int i = 0; i < number_of_flips; i++){ 


     coin_flip = rand() % 2; 

     if(coin_flip == 0){ 
      cout << "Heads" << endl; 
      if(coin_flip == previous_flip){ 
        head_count = head_count + 1;  
      } 
      else{ 
       if(head_count > highest_head){ 
        highest_head = head_count;     
       } 

       head_count = 1;  
      } 
     } 

     if(coin_flip == 1){ 
      cout << "Tails" << endl; 
      if(coin_flip == previous_flip){ 
        tail_count = tail_count + 1;   
      } 
      else{ 
       if(tail_count > highest_tail){ 
         highest_tail = tail_count;    
       } 

       tail_count = 1;  
      } 
     } 
     previous_flip = coin_flip; 
    } 
    if(head_count > highest_head){ 
      highest_head = head_count;    
    } 
    if(tail_count > highest_tail){ 
      highest_tail = tail_count;    
    } 
    cout << "The longest run of heads is " << highest_head << endl; 
    cout << "The longest run of tails is " << highest_tail << endl; 

    system("pause"); 
    return 0; 
} 
+0

什麼問題?你會得到什麼錯誤? –

+0

對我來說很不錯 – levis501

+0

該程序不計算連勝權。 –

回答

3

你代碼沒有考慮到最後的連勝,因爲你只檢查highest_headhighest_tail下一個翻轉是不同的。在最後翻轉,沒有下一個翻轉。

由於這是作業,我不會建議如何解決您的代碼。

+0

感謝提示格雷格和我理解不建議確切的代碼。我讀了你的建議,仍然不明白你到底在說什麼。我用示例輸出更新了我的原始帖子。 –

+0

我會建議在程序運行時打印出更多內容。例如,在每個循環迭代上都有'cout << head_count <<「」<< highest_head <<「」<< tail_count <<「」<< highest_tail << endl;'。下一個技巧是,當你連續獲得兩個同樣的翻轉時,你只會增加計數。 –

+0

感謝Greg的提示!我相信我已經得到它的工作。在執行你的建議後,我注意到如果最高計數是最後一個計數,最高計數沒有被存儲。我用我認爲是固定代碼的方式修改了我的帖子。 –

1

要添加到Greg的答案,如果previous_flip初始化爲0(這可能是因爲你沒有明確地自己做,也可能是其他任何東西,但通常在調試中它是0),並且第一次翻轉爲1 ,你的點數也會減少一個。

有幾個錯誤,但我不會張貼代碼。

首先,你永遠不會計算出與最後一次不同的第一次翻轉。只有在當前翻轉等於最後一次翻轉時,您纔會增加翻轉次數。在那裏,你錯過了一個。

然後,只有在當前翻轉不等於最後一個翻轉時,才設置最大連拍數。接下來會發生的是,你只能正確計數第一個條紋(假設你計數第一個右邊的右邊),因爲第二個條紋只有當它返回到同一個翻轉時纔會更新其最大條紋。以下是您提供的序列發生的情況:

Tails // Does not count this one because flip != last_flip 
Tails // tail_count is 1 
Tails // tail_count is 2 
Heads // Does not count first flip on flip switch, reset head_count to 0 
Heads // head_count is 1 
Tails // Does not count first flip, set max_tail to 2, reset tail_count to 0 
Tails // tail_count is 1 
Tails // tail_count is 2 
Tails // tail_count is 3 but will never be set unless we flip head, then tail. 
Heads // Does not count first switch, set max_head to 1, reset head_count to 0 

現在修復該算法。

+0

已更新的答案。 –

+0

感謝Eric的視覺效果。我相信我已經修復了我的代碼。我將代碼添加到原始帖子中。你可以看一下嗎? –

+0

這不是我怎麼做,但如果你測試它,它會返回正確的數字,我想這是沒問題的。 –