查看單個文章
舊 2005-08-30, 06:45 PM   #1
kcfs983
註冊會員
榮譽勳章

勳章總數0
UID - 12955
在線等級: 級別:0 | 在線時長:0小時 | 升級還需:5小時
註冊日期: 2002-12-16
VIP期限: 2006-02
文章: 72
精華: 0
預設 擲骰子的問題

請問各位大大幾個程式的問題

使用亂數產生一長度為n,範圍介於1~6的亂數陣列,並將該陣列以函數参數的方式回傳,來模擬擲了n次骰子的結果。

#include "stdio.h"
#include "stdlib.h"

// dist[6] : given distribution of each number's appearances
// result[n] : simulation result
// n : rolling a dice n times
//
void rolling(int dist[6], int result[][2], int n) {

//在這加入程式碼 }

void print(int vals[][2], int len) {
int i;
int cnt[6];
for(i=0; i<6; i++)
cnt[i]=0;
printf("Rolling result : ");
for(i=0; i<len; i++) {
printf("(%d,%d) ",vals[i][0],vals[i][1]);
int index=vals[i][0]-1;
cnt[index]++;
index=vals[i][1]-1;
cnt[index]++;
}
printf("\n");
for(i=0; i<6; i++) {
printf("\tdice(%d) = %4.1f \n",i+1,(double)cnt[i]/len*100);
}
printf("\n");
}

int main(){

const int n=100;
int dices[n][2];
int dist1[] = {3,3,3,6,6,12};
int dist2[] = {6,3,6,1,1,12};
int dist3[] = {1,1,2,1,1,4};

srand(time(NULL));
rolling(dist1,dices,n);
print(dices,n);
rolling(dist2,dices,n);
print(dices,n);
rolling(dist3,dices,n);
print(dices,n);

return 0;
}
kcfs983 目前離線  
送花文章: 0, 收花文章: 0 篇, 收花: 0 次
回覆時引用此帖