C++ C++;跟踪最长的连续记录

C++ C++;跟踪最长的连续记录,c++,C++,在我的计算机编程课上,我决定制作一个程序,生成一个随机抛硬币的过程,并记录正面和反面最长的连续连击。我在网上搜索了一下,没有找到答案。计数显示不正确。哪怕只是一个暗示都会很棒! 谢谢 贾斯汀 作为参考,以下是我认为现在有效的最终代码: int main(){ int number_of_flips; int coin_flip; int previous_flip = 2; int head_count = 0; int tail_count = 0;

在我的计算机编程课上,我决定制作一个程序,生成一个随机抛硬币的过程,并记录正面和反面最长的连续连击。我在网上搜索了一下,没有找到答案。计数显示不正确。哪怕只是一个暗示都会很棒! 谢谢 贾斯汀

作为参考,以下是我认为现在有效的最终代码:

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;
}
intmain(){
翻转的整数;
掷硬币;
int-previous_-flip=2;
int head_count=0;
int tail_count=0;
int最高水头=0;
int最高_尾=0;
srand(时间(空));
翻转次数;
系统(“cls”);
对于(int i=0;i<翻转次数;i++){
coin_flip=rand()%2;
如果(硬币翻转==0){
库特(尾巴){
最高尾数=尾数;
}

cout您的代码不考虑最后一个条纹,因为您仅在下一次翻转不同时检查
最高头部
最高尾部
。在上一次翻转时,没有下一次翻转


因为这是家庭作业,所以我将不建议如何修复代码。

您的代码没有考虑最后一个条纹,因为您仅在下一次翻转不同时检查
最高头部
最高尾部
。在上一次翻转时,没有下一次翻转


由于这是家庭作业,我将避免建议如何修复您的代码。

要添加到Greg答案中,如果
上一次翻转
被初始化为0(可能是因为您自己没有显式地进行翻转,也可能是其他任何操作,但通常在调试中它是0),并且您的第一次翻转是1,那么您的计数也将减少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 

现在修复该算法。

要添加到Greg answer,如果
上一次翻转
被初始化为0(这可能是因为您自己没有显式地执行,它也可能是其他任何操作,但通常在debug中它是0),并且您的第一次翻转是1,那么您的计数也将减少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 


现在修复该算法。

问题是什么?你会得到什么错误?程序没有正确计算条纹。在指定之前,你使用的是
previous\u flip
;它可能包含一些随机值(尽管读取它在技术上是错误的)感谢Seth,我将其初始化为2,并修复了计数被1关闭的问题。问题是什么?你会得到什么错误?程序没有正确计算条纹。在指定之前,你使用的是
previous\u flip
;它可能包含一些随机值(尽管读取它在技术上是不正确的)谢谢Seth,我将其初始化为2,并修复了计数被1关闭的问题。谢谢Greg的提示,我理解没有建议使用确切的代码。我阅读了你的建议,但仍然不理解你的确切意思。我用一个示例输出更新了我的原始帖子。我建议打印更多正在进行的内容在程序运行时在程序内部。例如,
不能感谢Greg的提示!我相信我已经让它工作了。在将您建议的内容实现到我的循环中后,我注意到如果最高计数是最后一次计数,则最高计数没有被存储。我用我认为是固定代码的内容修改了我的帖子。感谢Greg和我的提示derstand不建议使用确切的代码。我阅读了你的建议,但仍然不明白你到底在说什么。我用一个示例输出更新了我的原始帖子。我建议在程序运行时打印出更多程序内部的内容。例如,
cout感谢你的提示Greg!我相信我已经让它工作了。之后在我的循环中实现了你的建议,我注意到如果最高计数是最后一次计数,那么最高计数就不会被存储。我用我认为是固定代码的内容修改了我的帖子。感谢Eric提供的视觉效果。我相信我已经固定了我的代码。我将代码添加到了原始帖子中。你能看一下吗?这不是我该怎么做,但如果你你测试它,它返回正确的数字,我想它是正确的。它确实返回正确的数字。你建议怎么做?我不会在这个任务中使用你建议的代码,我只想知道你将如何解决这个问题。我个人会增加计数,并设置最大值,如果需要的话,在每一种翻转,我会重置count在另一种类型上。类似这样:增加这种类型,如果需要设置最大值,重置另一种类型,因为这是一个开关。你可以跳过循环出口处的检查,不需要记住最后一次翻转。啊,这很有意义。我感谢你的建议。谢谢你的视觉效果Eric。我相信我已经修复了我的代码。我将代码添加到了o原来的帖子,你可以吗
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