#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main( ) {
int PORTAS [ ] = { 0 , 0 , 0 } ; // 0 - a porta contém uma cabra; 1 - a porta contém o carro
int CAR, ESC, NUM, DECISAO, AUX;
float VIT, DER;
int ESTAT [ ] = { 0 , 0 , 0 } ; // {Tentativas, vitórias, derrotas}
int repeticoes;
printf ( "1 para trocar todas, 0 para manter todas: " ) ; printf ( "Digite o número de repetições: " ) ; // Recomendável 10, 100 ou 1000. scanf ( "%d" , & repeticoes
) ; NUM = repeticoes;
do {
PORTAS[ 0 ] = PORTAS[ 1 ] = PORTAS[ 2 ] = 0 ;
CAR
= rand ( ) % 3 ; // Randomiza a porta que recebe o carro. PORTAS [ CAR] = 1 ;
ESC
= rand ( ) % 3 ; // Randomiza a escolha da porta.
if ( DECISAO == 1 ) { // Se foi escolhido trocar todas as vezes.
if ( PORTAS [ ESC] == 1 ) { // Porta escolhida contém o carro.
for ( AUX = 0 ; AUX < 3 ; AUX++ ) {
if ( PORTAS [ AUX] != 1 && AUX != ESC ) {
ESC = AUX; //Mudança de porta
AUX = 3 ; // Para quebrar o 'For'
ESTAT [ 2 ] += 1 ;
}
}
}
else if ( PORTAS [ ESC] == 0 ) { //Porta escolhida contém uma cabra.
ESC = CAR; // Pois sendo a porta errada, e tendo a outra errada sido revelada, só sobrou a correta.
ESTAT [ 1 ] += 1 ;
}
}
if ( DECISAO == 0 ) { //Caso tenha sido escolhido manter todas as vezes.
if ( ESC == CAR ) {
ESTAT [ 1 ] += 1 ;
}
else {
ESTAT [ 2 ] += 1 ;
}
}
NUM--;
ESTAT [ 0 ] += 1 ;
} while ( NUM > 0 ) ;
VIT = ( 1.0f * ESTAT[ 1 ] / repeticoes ) * 100 ; // Porcentagem de vitórias *ERRO*
DER = ( 1.0f * ESTAT[ 2 ] / repeticoes ) * 100 ; // Porcentagem de derrotas *ERRO*
( DECISAO
== 1 ) ? printf ( "\n \n \n \t Trocando de porta todas as vezes: \n \n " ) : printf ( "\n \n \n \t Mantendo a porta todas as vezes: \n \n " ) ; printf ( "Número de tentativas: %d\n " , ESTAT
[ 0 ] ) ; printf ( "Número de vitórias: %d, %f%% do total.\n " , ESTAT
[ 1 ] , VIT
) ; printf ( "Número de derrotas: %d, %f%% do total." , ESTAT
[ 2 ] , DER
) ; return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCmludCBtYWluKCl7CiAgICBpbnQgUE9SVEFTIFtdID0gezAsIDAsIDB9OyAgLy8gMCAtIGEgcG9ydGEgY29udMOpbSB1bWEgY2FicmE7IDEgLSBhIHBvcnRhIGNvbnTDqW0gbyBjYXJybyAKICAgIGludCBDQVIsIEVTQywgTlVNLCBERUNJU0FPLCBBVVg7CiAgICBmbG9hdCBWSVQsIERFUjsgCiAgICBpbnQgRVNUQVQgW10gPSB7MCwgMCwgMH07ICAgLy8ge1RlbnRhdGl2YXMsIHZpdMOzcmlhcywgZGVycm90YXN9CiAgICBpbnQgcmVwZXRpY29lczsKCiAgICBwcmludGYoIjEgcGFyYSB0cm9jYXIgdG9kYXMsIDAgcGFyYSBtYW50ZXIgdG9kYXM6ICIpOwogICAgc2NhbmYgKCIlZCIsICZERUNJU0FPKTsKICAgIHByaW50ZiAoIkRpZ2l0ZSBvIG7Dum1lcm8gZGUgcmVwZXRpw6fDtWVzOiAiKTsgLy8gUmVjb21lbmTDoXZlbCAxMCwgMTAwIG91IDEwMDAuCiAgICBzY2FuZiAoIiVkIiwgJnJlcGV0aWNvZXMpOwogICAgTlVNID0gcmVwZXRpY29lczsKICAgIGRvewogICAgCVBPUlRBU1swXSA9IFBPUlRBU1sxXSA9IFBPUlRBU1syXSA9IDA7CiAgICAgICAgQ0FSID0gcmFuZCAoKSAlMzsgICAvLyBSYW5kb21pemEgYSBwb3J0YSBxdWUgcmVjZWJlIG8gY2Fycm8uCiAgICAgICAgUE9SVEFTIFtDQVJdID0gMTsgICAKCiAgICAgICAgRVNDID0gcmFuZCAoKSAlMzsgICAvLyBSYW5kb21pemEgYSBlc2NvbGhhIGRhIHBvcnRhLgoKICAgICAgICBpZiAoIERFQ0lTQU8gPT0gMSApeyAgICAvLyBTZSBmb2kgZXNjb2xoaWRvIHRyb2NhciB0b2RhcyBhcyB2ZXplcy4KICAgICAgICAgICAgaWYgKCBQT1JUQVMgW0VTQ10gPT0gMSApeyAgIC8vIFBvcnRhIGVzY29saGlkYSBjb250w6ltIG8gY2Fycm8uCiAgICAgICAgICAgICAgICBmb3IgKCBBVVggPSAwOyBBVVggPCAzIDsgQVVYKysgKXsKICAgICAgICAgICAgICAgICAgICBpZiAoIFBPUlRBUyBbQVVYXSAhPSAxICYmIEFVWCAhPSBFU0MgKXsKICAgICAgICAgICAgICAgICAgICAgICAgRVNDID0gQVVYOyAgICAgIC8vTXVkYW7Dp2EgZGUgcG9ydGEKICAgICAgICAgICAgICAgICAgICAgICAgQVVYID0gMzsgICAgLy8gUGFyYSBxdWVicmFyIG8gJ0ZvcicKICAgICAgICAgICAgICAgICAgICAgICAgRVNUQVQgWzJdICs9IDE7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICBlbHNlIGlmICggUE9SVEFTIFtFU0NdID09IDApeyAgICAvL1BvcnRhIGVzY29saGlkYSBjb250w6ltIHVtYSBjYWJyYS4KICAgICAgICAgICAgICAgIEVTQyA9IENBUjsgIC8vIFBvaXMgc2VuZG8gYSBwb3J0YSBlcnJhZGEsIGUgdGVuZG8gYSBvdXRyYSBlcnJhZGEgc2lkbyByZXZlbGFkYSwgc8OzIHNvYnJvdSBhIGNvcnJldGEuCiAgICAgICAgICAgICAgICBFU1RBVCBbMV0gKz0gMTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaWYgKCBERUNJU0FPID09IDApeyAgICAgLy9DYXNvIHRlbmhhIHNpZG8gZXNjb2xoaWRvIG1hbnRlciB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