//C++로 시작하는 객체지향프로그래밍 p.218 예제 5.40 - (시뮬레이션: 앞면 또는 뒷면) 동전을 백만 번 던지는 것을 시뮬레이션하고 앞면과 뒷면의 수를 출력하는 프로그램을 작성하여라.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
long x;
int count1 =0, count2=0;
srand(time(0));
for (int i = 0; i < 1000000; i++){
x = rand()%2 + 1;
if (x == 1)
count1++;
else if (x==2)
count2++;
}
cout << "앞면의 수는" << count1 << endl;
cout << "뒷면의 수는" << count2 << endl;
system("pause");
return 0;
}
댓글 없음:
댓글 쓰기