Mathematical logic betting on the lottery

1

I can not think of the mathematical logic of the following question:

Three friends played in the lottery. If they win, the prize must be apportioned in proportion to the value that each gave to the bet.

  

The goal is to make a program that reads the value that each bettor invested and the value of the prize and in the end, inform how much each bettor would win the prize based on the amount invested.

To facilitate I have only the pseudocode outline.

float aposta1, aposta2, aposta3, premio, recebe1, recebe2, recebe3;
leia aposta1, aposta2, aposta3, premio;

The great difficulty for me is that there is no pre-defined range of bet values, making it difficult to create the algorithm.

  

In the algorithm I should preferably use fairly basic programming content, if possible even without conditionals.

    
asked by anonymous 09.04.2018 / 14:50

2 answers

4

Based on the responses of @David Dias and @Leo Caracciolo, I come to the simple conclusion that a good solution is to do the following:

Make the sum of all bet amounts, in the form:

apostaTotal = aposta1 + aposta2 + aposta3;

And to find the corresponding percentage, simply divide the bet by the total value of the bet. Example:

porcentagem1 = aposta1 / apostaTotal;

And then, to have the individual value of the prize is just multiply the value of the prize by the invested percentage of each bettor.

Below is the resolution of the question with an example code written in the C language:

#include <stdio.h>
int main(){
    float aposta1, aposta2, aposta3, premio, p1, p2, p3, apostaTotal, recebe1, recebe2, recebe3;
    scanf("%f%f%f%f",&aposta1, &aposta2, &aposta3, &premio);

    apostaTotal = aposta1 + aposta2 + aposta3;
    p1 = aposta1 / apostaTotal;
    p2 = aposta2 / apostaTotal;
    p3 = aposta3 / apostaTotal;

    recebe1 = premio * p1;
    recebe2 = premio * p2;
    recebe3 = premio * p3;

    printf("\nO apostador 1 recebe: %f\n", recebe1);
    printf("O apostador 2 recebe: %f\n", recebe2);
    printf("O apostador 3 recebe: %f\n", recebe3);
    return 0;
}
    
09.04.2018 / 15:56
2

It would look like this without a pseudo code.

  var Premio, Apostador01, Apostador02, Apostador01, ApostadorPc01, ApostadorPc02,ApostadorPc03: float;
  leia  Premio
  leia  Apostador01;
  leia  Apostador02;
  leia  Apostador03;
  Aposta := Apostador01+Apostador02+Apostador03;

  ApostadorPc01  := (Apostador01.Value*100)/Aposta;
  ApostadorPc02  := (Apostador02.Value*100)/Aposta;
  ApostadorPc03  := (Apostador03.Value*100)/Aposta;

  Escreva 'porcentagem do Apostador1'+ ApostadorPc01  +'%';
  Escreva 'porcentagem do Apostador2'+ ApostadorPc02  +'%';
  Escreva 'porcentagem do Apostador3'+ ApostadorPc03  +'%';

  Escreva 'Premio do Apostador1'+ ((Premio*ApostadorPc01)/100);
  Escreva 'Premio do Apostador2'+ ((Premio*ApostadorPc02)/100);
  Escreva 'Premio do Apostador3'+ ((Premio*ApostadorPc03)/100);
    
09.04.2018 / 20:54