对于我的计算机编程课程,我决定制作一个程序,生成随机硬币翻转并跟踪头部和尾部最长的连续条纹。我搜索了互联网,没有找到答案。计数显示不正确。即使只是一个提示也会很棒! 谢谢, 贾斯汀
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 :(得分:3)
您的代码未考虑最后条纹,因为您只在 next 翻转时检查highest_head
或highest_tail
不同。在最后一次翻转时,没有下一次翻转。
由于这是作业,我将不会建议如何修复你的代码。
答案 1 :(得分: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
现在修复该算法。