fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
Success #stdin #stdout 0.09s 27756KB
stdin
int set_door()
{
 //0か1か2を乱数で返す
 int a;
 a = rand() % 3;
 return a;
}

int select_door(int a, int b)
{
 //0か1か2のうち、引数a,bに
 //受け取っていない番号を返す
 int c;
 switch (a * 3 + b){
 case 0: return rand % 2 + 1;
 case 1: return 2;
 case 2: return 1;
 case 3: return 2;
 case 4: return rand % 2 * 2;
 case 5: return 0;
 case 6: return 1;
 case 7: return 0;
 case 8: return rand % 2;
 }
       return c;
}

int main()
{
 //モンティ・ホール問題
 int you; //あなたのドア
 int ans; //当たりのドア
 int opn; //開けられるドア
 int change = 1; //1のときドアを変更する
 int chk = 0; //正解の回数
 int j;
 int n = 1000; //ゲームの回数

 //Seed for random number 
 time_t tim;
 time(&tim);
 srand((unsigned)tim);

 for (j = 0; j < n; j++){
  you = 0; //学籍番号下1桁を3で割った余りにすること

  printf("Your choise is %d\n", you);

  ans = set_door(); //当たりのドア番号

  opn = select_door(you,ans); //開けられるドア番号
  printf("%d is NOT an answer\n", opn);

  if (change == 1) { //ドアを変更する
   you = select_door(you,opn);
   printf("Your choise is changed to %d\n", ans);
  }

  //答え合わせ
  if (you = ans){
   puts("You win!");
   chk++;
  }
  else {
   printf("You lose... The answer is %d\n", ans);
  }
  puts("");
 }
 printf("%d/%d wins\n", chk, n);
 //1000回中689回当たった

 return 0;
}
stdout
Standard output is empty