gpt4 book ai didi

c - 无法获得有关在 C 中滚动自然 yahtzee 的统计信息

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:14 25 4
gpt4 key购买 nike

我正在为学校的一项作业制作这个程序,但是我们需要获得获得 Yahtzee 所需的最高掷骰数和获得 Yahtzee 所需的最少掷骰数。然后我们需要找到平均卷数。目前,我对确定获得 Yahtzee 的最高和最低卷数出错的地方感到困惑。任何人都可以帮助我,我将不胜感激。该程序一直运行到获得 100 个天然 Yahtzees。

#include <stdio.h> 
#include <stdlib.h>
#include <time.h>
#include <limits.h>

int main( void )
{ // MARKS THE BEGINNING OF THE main( ) BLOCK OF STATEMENTS

int die1 = 0 , die2 = 0 , die3 = 0 , die4 = 0, die5 = 0 , roll = 0 ;
int lownr = INT_MAX , highnr = 0 , yahtzee = 0 , averagenr;
int roll_total = 0 ;
srandom ( (unsigned) time (NULL) ) ;

printf( "\nWelcome User, this program demonstrates the " ) ;
printf( "number of rolls it takes to obtain" ) ;
printf( "\na natural Yahtzee.\n" ) ;

die1 = random ( ) % 6 + 1;
die2 = random ( ) % 6 + 1;
die3 = random ( ) % 6 + 1;
die4 = random ( ) % 6 + 1;
die5 = random ( ) % 6 + 1;

printf( "\nYou rolled: %d, %d, %d, %d, %d.\n\n" , die1 ,
die2 , die3 , die4 , die5 ) ;

while (yahtzee < 100) {

++roll;
++roll_total;

die1 = random ( ) % 6 + 1;
die2 = random ( ) % 6 + 1;
die3 = random ( ) % 6 + 1;
die4 = random ( ) % 6 + 1;
die5 = random ( ) % 6 + 1;


if ((die1==die2) && (die2==die3) && (die3==die4) && (die4==die5)) {

yahtzee = yahtzee +1;

/* If you have yahtzee, compare roll with previous values of highnr/lownr */
if (roll>highnr)
highnr = roll;
if (roll<lownr)
lownr = roll;

/* Reset the roll counter, for next yahtzee */
roll = 0;
}

}
averagenr = roll_total / yahtzee ;


printf("\n The total number of yahtzees you rolled is: %d.",yahtzee);
printf("\n The total number of times you rolled is: %d.",roll_total);
printf("\n The highest number of rolls to get a yahtzee is: %d",highnr);
printf("\n The lowest number of rolls to get a yahtzee is: %d", lownr);
printf("\n The average number of rolls to obtain a yahtzee is: %d",
averagenr);
printf ( "\n\n Thank you for using this program. " ) ;
printf ( "GoodBye.\n\n " ) ;

return ( 0 ) ;

}

示例输出

Welcome User, this program demonstrates the number of rolls it takes to obtain
a natural Yahtzee.

You rolled: 5, 5, 3, 2, 1.


The total number of yahtzees you rolled is: 100.
The total number of times you rolled is: 116704.
The highest number of rolls to get a yahtzee is: 5284
The lowest number of rolls to get a yahtzee is: 18
The average number of rolls to obtain a yahtzee is: 1167

Thank you for using this program. GoodBye.

已解决

最佳答案

据我了解,关键是您需要使用两个计数器来计算卷数。一个用于滚动总数,稍后您将使用它来计算平均值,另一个用于跟踪下一个 yahtzee 的当前滚动数。

int roll=0, roll_total=0;
int lownr=INT_MAX, highnr=0, averagenr;

while(yahtzee < 100) {

++roll; /* This will keep track of the number of rolls towards next yahtzee */
++roll_total; /* This is total number of rolls in entire program */

if ((die1==die2) && (die2==die3) && (die3==die4) && (die4==die5)) {

yahtzee = yahtzee +1;

/* If you have yahtzee, compare roll with previous values of highnr/lownr */
if (roll>highnr)
highnr = roll;
if (roll<lownr)
lownr = roll;

/* Reset the roll counter, for next yahtzee */
roll = 0;
}
}

averagenr = roll_total / yahtzee ;

编辑

我用另一条评论回答了你的评论,但评论中的代码变得不可读,所以我会帮助你更多。仔细查看更新后的代码并从您的代码中删除这些行:

lownr = roll;
highnr = roll;

关于c - 无法获得有关在 C 中滚动自然 yahtzee 的统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19233371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com