#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 {
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 ;
}
}
}
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 = ( ESTAT[ 2 ] / repeticoes ) * 100 ; // Porcentagem de vitórias *ERRO*
DER = ( ESTAT[ 3 ] / 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+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCmludCBtYWluKCl7CiAgICBpbnQgUE9SVEFTIFtdID0gezAsIDAsIDB9OyAgLy8gMCAtIGEgcG9ydGEgY29udMOpbSB1bWEgY2FicmE7IDEgLSBhIHBvcnRhIGNvbnTDqW0gbyBjYXJybyAKICAgIGludCBDQVIsIEVTQywgTlVNLCBERUNJU0FPLCBBVVg7CiAgICBmbG9hdCBWSVQsIERFUjsgCiAgICBpbnQgRVNUQVQgW10gPSB7MCwgMCwgMH07ICAgLy8ge1RlbnRhdGl2YXMsIHZpdMOzcmlhcywgZGVycm90YXN9CiAgICBpbnQgcmVwZXRpY29lczsKCiAgICBwcmludGYoIjEgcGFyYSB0cm9jYXIgdG9kYXMsIDAgcGFyYSBtYW50ZXIgdG9kYXM6ICIpOwogICAgc2NhbmYgKCIlZCIsICZERUNJU0FPKTsKICAgIHByaW50ZiAoIkRpZ2l0ZSBvIG7Dum1lcm8gZGUgcmVwZXRpw6fDtWVzOiAiKTsgLy8gUmVjb21lbmTDoXZlbCAxMCwgMTAwIG91IDEwMDAuCiAgICBzY2FuZiAoIiVkIiwgJnJlcGV0aWNvZXMpOwogICAgTlVNID0gcmVwZXRpY29lczsKICAgIGRvewogICAgICAgIENBUiA9IHJhbmQgKCkgJTM7ICAgLy8gUmFuZG9taXphIGEgcG9ydGEgcXVlIHJlY2ViZSBvIGNhcnJvLgogICAgICAgIFBPUlRBUyBbQ0FSXSA9IDE7ICAgCgogICAgICAgIEVTQyA9IHJhbmQgKCkgJTM7ICAgLy8gUmFuZG9taXphIGEgZXNjb2xoYSBkYSBwb3J0YS4KCiAgICAgICAgaWYgKCBERUNJU0FPID09IDEgKXsgICAgLy8gU2UgZm9pIGVzY29saGlkbyB0cm9jYXIgdG9kYXMgYXMgdmV6ZXMuCiAgICAgICAgICAgIGlmICggUE9SVEFTIFtFU0NdID09IDEgKXsgICAvLyBQb3J0YSBlc2NvbGhpZGEgY29udMOpbSBvIGNhcnJvLgogICAgICAgICAgICAgICAgZm9yICggQVVYID0gMDsgQVVYIDwgMyA7IEFVWCsrICl7CiAgICAgICAgICAgICAgICAgICAgaWYgKCBQT1JUQVMgW0FVWF0gIT0gMSAmJiBBVVggIT0gRVNDICl7CiAgICAgICAgICAgICAgICAgICAgICAgIEVTQyA9IEFVWDsgICAgICAvL011ZGFuw6dhIGRlIHBvcnRhCiAgICAgICAgICAgICAgICAgICAgICAgIEFVWCA9IDM7ICAgIC8vIFBhcmEgcXVlYnJhciBvICdGb3InCiAgICAgICAgICAgICAgICAgICAgICAgIEVTVEFUIFsyXSArPSAxOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQoKICAgICAgICAgICAgaWYgKCBQT1JUQVMgW0VTQ10gPT0gMCl7ICAgIC8vUG9ydGEgZXNjb2xoaWRhIGNvbnTDqW0gdW1hIGNhYnJhLgogICAgICAgICAgICAgICAgRVNDID0gQ0FSOyAgLy8gUG9pcyBzZW5kbyBhIHBvcnRhIGVycmFkYSwgZSB0ZW5kbyBhIG91dHJhIGVycmFkYSBzaWRvIHJldmVsYWRhLCBzw7Mgc29icm91IGEgY29ycmV0YS4KICAgICAgICAgICAgICAgIEVTVEFUIFsxXSArPSAxOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBpZiAoIERFQ0lTQU8gPT0gMCl7ICAgICAvL0Nhc28gdGVuaGEgc2lkbyBlc2NvbGhpZG8gbWFudGVyIHRvZGFzIGFzIHZlemVzLgogICAgICAgICAgICBpZiAoIEVTQyA9PSBDQVIgKXsKICAgICAgICAgICAgICAgIEVTVEFUIFsxXSArPSAxOwogICAgICAgICAgICB9CiAgICA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