2012-01-26 45 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

这不是我怎么做,但如果你测试它,它会返回正确的数字,我想这是没问题的。 –