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

C언어 38제] C언어 콘서트 CHAPTER 6 Programming 18 목표에 도달할 확률 p254

by 건티 2021. 11. 30.
728x90

출처 : 반크(독도포스터)

 

참고풀이]

//Dev-C++ 5.11로 작업함.
#include <stdio.h>
#include <stdlib.h> //srand(), rand()
#include <time.h> //time()

int main()
{
   int cash;//게임어 금액변수 
   int target;//목표 금액변수
   int win;//1000번 중 이긴 횟수변수
   int bets;//배팅 변수
 

   //무작위로 값을 산출하도록 초기값을 설정한다.
   srand(time(NULL));

   cash=50;
   target=250;
   bets=win=0;

   printf("초기 금액 $%d\n",cash);
   printf("목표 금액 $%d\n",target);
   while(1)
   {
      bets++;
      if(cash==0 || cash>=target) break;
      if(rand()%2==1) 
      {
         cash++;
         if(bets<=1000) win++;
      }
      else cash--;
   }

   printf("1000번 중 %d번 승리\n",win);
   printf("이긴 확률 = %lf\n",(double)win/10.);

   return 0;
}

 

참고풀이 결과]

 

 

 

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

 

반응형

댓글