2007年11月26日 星期一

小考三(F) 解答


/* C Programming Quiz 3F */
/*
小考三題目:使用兩個骰子,在丟出10000次之後,計算出現
點數和為 2 或 12 的機率。
提示一:使用兩個隨機變數。
提示二:以 randNum1+randNum2 == 2 || randNum1+randNum2 == 12
來累進出現的次數,再除以10000,即可得此機率。
輸入值:無。
輸出值:一個機率值。
*/
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int randNum1, randNum2;
int i;
double count=0;
srand(time(NULL));
for (i=0; i<10000; i++)
{
randNum1 = rand()%6+1;
randNum2 = rand()%6+1;
if (randNum1+randNum2 == 2 || randNum1+randNum2 == 12)
count++;
}
printf("The probability of obtaining 2 or 12 is %.3lf\n", count/10000);

system("pause");
return 0;
}

小考三(F)題目
返回小考目錄
回到首頁

沒有留言: