>>420 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i, j, targetMoney, startMoney, currentMoney;
int totalGame = 10000, totalDice = 0, totalWin = 0;
srand(time(NULL));
printf("所持金を入力してください:");
scanf("%d", &startMoney);
printf("目標額を入力してください:");
scanf("%d", &targetMoney);
for(i=0; i<totalGame; i++) {
for(j=1, currentMoney = startMoney; ; j++) {
if((int)(rand() / (RAND_MAX + 1.0) * 6) & 1 ) currentMoney++;
else currentMoney--;
if(currentMoney == targetMoney) { totalWin++; break; }
if(currentMoney == 0) break;
}
totalDice += j;
}
printf("勝率%.2f%% 平均回数%.2f\n", 100.0 * totalWin / totalGame, (double)totalDice / totalGame);
return 0;
}