본문 바로가기
프로그램/C++ 1000제

C++ 94제] 어서와 C++는 처음이지! CHAPTER 02 PROGRAMMING EXERCISE 16.

by 건티 2022. 10. 10.
728x90

출처 : 반크_세계유산 경복궁

 

참고 풀이]

#include <iostream>
#include <cstdlib> //srand(), rand()
#include <ctime> //time()
using namespace std;

int main()
{
   int cash=50;//초기금액과 $1증감변수 
   int bets;//배팅횟수 체크변수 
   int win;//1000번 중 이긴 횟수 체크변수 

   srand(time(NULL));

   cout << "초기 금액 : $" << cash << endl;
   cout << "목표 금액 : $250" << endl;

   //cash가 0이거나 $250에 도달하면 작업을 중지한다.
   bets=win=0;
   while(true) 
   {
      if(cash<=0 || cash>=250) break;
      bets++;

      if((double)rand()/RAND_MAX<0.5)
      {
         cash++;
         if(bets<=1000) win++;
      } 
      else cash--;
   }

   cout << "1000 중의 " << win << "번 승리\n";
   cout << "이긴 확률=" ;
   cout << fixed;
   cout.precision(6);
   cout << (double)win/10. << endl;
   cout << "평균 배팅 횟수 = ";
   cout << (double)bets/1.0 << endl; 

   return 0;
}

 

참고풀이 결과]

 

 

 

 

대한민국의 아름다운 영토, 독도의 가을

 

반응형

댓글