-
Notifications
You must be signed in to change notification settings - Fork 18
/
8_14模拟骰子.cpp
51 lines (46 loc) · 1001 Bytes
/
8_14模拟骰子.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define N 6000
//习题8_14
//模拟骰子
int main(){
srand(time(NULL));
int i,a,s1=0,s2=0,s3=0,s4=0,s5=0,s6=0;
for (i=0;i<N;i++){
a = rand() % 6 + 1;
switch (a){
case 1:
s1++;
break;
case 2:
s2++;
break;
case 3:
s3++;
break;
case 4:
s4++;
break;
case 5:
s5++;
break;
case 6:
s6++;
break;
default:
break;
}
}
cout << "1的概率:" << (float)s1/N << endl;
cout << "2的概率:" << (float)s2/N << endl;
cout << "3的概率:" << (float)s3/N << endl;
cout << "4的概率:" << (float)s4/N << endl;
cout << "5的概率:" << (float)s5/N << endl;
cout << "6的概率:" << (float)s6/N << endl;
return 0;
}