Exercise: Pair or odd championship in c

Asked

Viewed 268 times

2

I believe I’m not printing the result, because the vector condition is not met as a consequence of the value of the variable Winner, but I can’t see solution since I can only use these resources. I cannot use arrays or repetitions.

#include <stdio.h>

int main() {
    int dec1, p1, p2, dec2, j1, j2, result1, result2, def, f1, f2, winner;

    scanf("%d %d %d %d %d %d %d %d %d", &dec1, &p1, &p2, &dec2, &j1, &j2, &def, &f1, &f2);

    /*
    p1 = A
    P2 = B
    J1 = C
    J2 = D
    */

    if (dec1 == 0) {
        if (p1 == 0 || p1 == 2 || p1 == 4 || p1 == 6 || p1 == 8 || p1 == 10)
        result1 = p1;
    } else {
        result1 = p2;
    }
    if (dec2 == 0) {
        if (j1 == 0 || j1 == 2 || j1 == 4 || j1 == 6 || j1 == 8 || j1 == 10)
        result2 = j1;
    } else {
        result2 = j2;
    }
    if (def == 0) {
        if (f1 == 0 || f1 == 2 || f1 == 4 || f1 == 6 || f1 == 8 || f1 == 10)
        winner = result1;
    } else {
        winner = result2;
    }
    if (winner == p1) {
        printf("A\n");
    } else if (winner == p2) {
        printf("B\n");
    } else if (winner == j1) {
        printf("C\n");
    } else if (winner == j2) {
        printf("D\n");
    }

    return 0;
}

This is the exercise link: https://moj.naquadah.com.br/contests/bcrjlsi3alistaapc20192/torneioparimpar_simplificado.pdf

2 answers

3


I can’t understand the logic you use to find the winner.

You check whether player 1 chose pair or odd, and then you set the winner based on player 1’s fingers only, and store the amount of the winner’s fingers as a result rather than storing a reference to the player. That’s it?

This whole logic needs to be reworked.

You could have something like

scanf("%d %d %d", &e, &j1, &j2);
winner = (j1 + j2) % 2 == e ? 'A' : 'B';

That is, if the remnant of the sum between the number of fingers between player 1 and player 2, divided by 2; equal to what player 1 has chosen (0 = even, 1 = odd), player 1 (A) wins, otherwise player 2 (B) wins.

In such a way, the whole logic of the exercise can be condensed into a code like the following

// Armazeno o vencedor do primeiro jogo
scanf("%d %d %d", &e, &j1, &j2);
winner1 = (j1 + j2) % 2 == e ? 'A' : 'B';

// Armazeno o vencedor do segundo jogo
scanf("%d %d %d", &e, &j1, &j2);
winner2 = (j1 + j2) % 2 == e ? 'C' : 'D';

// Defino o vencedor final com base nos vencedores dos jogos anteriores
scanf("%d %d %d", &e, &j1, &j2);
winner3 = (j1 + j2) % 2 == e ? winner1 : winner2;

printf("%c\n", winner3);

The complete program would be

#include <stdio.h>

int main() {
    int e, j1, j2;
    char winner1, winner2, winner3;

    scanf("%d %d %d", &e, &j1, &j2);
    winner1 = (j1 + j2) % 2 == e ? 'A' : 'B';

    scanf("%d %d %d", &e, &j1, &j2);
    winner2 = (j1 + j2) % 2 == e ? 'C' : 'D';

    scanf("%d %d %d", &e, &j1, &j2);
    winner3 = (j1 + j2) % 2 == e ? winner1 : winner2;

    printf("%c\n", winner3);

    return 0;
}
  • Thank you very much, really this went unnoticed.

2

I went to read the statement and started to do the implementation only I ended up traveling too much in the codes and I made an interactive game between the player and the computer kkkk, if anyone is interested here is the implementation:

#include <stdio.h>

#include <stdlib.h>
#include <time.h>

#include <string.h>

// Obtem um número.
int getNumber() {
  int number = 0;
  scanf("%d*c", &number);

  return number;
}

// Escreve a escolha do jogador.
void printChoice(const char* player, int choice, const char* adversary) {
  const char *playerChoice = (choice) ? "impar" : "par";
  const char *adversaryChoice = (!choice) ? "impar" : "par";

  printf("O jogador %s escolheu %s então o adversário %s ficará com %s.\n", player, playerChoice, adversary, adversaryChoice);
}

// Obtem um número aleatorio entre 0 e max.
int getRandomNumber(int max) {
  srand(time(NULL));

  int number = rand() % max;

  return number;
}

// Escreve os números escolhidos pelo jogador e o adversario.
void printNumbers(const char* player, int playerNumber, const char* adversary, int adversaryNumber) {
  printf("O jogador %s escolheu %d.\n", player, playerNumber);
  printf("O adversário %s escolheu %d.\n", adversary, adversaryNumber);
}

// Escreve o resultado da soma entre o número do jogador e do adversario e se é par ou impar.
void printResult(int playerNumber, int adversaryNumber) {
  int result = playerNumber + adversaryNumber;

  printf("Resultado: %d é %s.\n", result, (result) % 2 == 0 ? "par" : "impar");
}

// Obtem o nome do vencedor.
const char* getWinner(const char* player, int playerNumber, int choice, const char* adversary, int adversaryNumber) {
  int result = playerNumber + adversaryNumber;

  // Se o resultado for impar e o player escolheu impar retorne o player se não retorne o adversary.
  // Se o resultado for par e o player escolheu par retorne o player se não retorne o adversary.

  return (result % 2) ? ((choice) ? player : adversary) : ((!choice) ? player : adversary); 
}

// Escreve o quem está competindo nesse round e o nome do vendedor.
void printWinner(const char* player, const char* adversary, const char* winner) {
  printf("Round(%s x %s): Vencedor %s.\n", player, adversary, winner);
}

int main() {
  printf("[TORNEIO DE PAR OU IMPAR]\n");

  char player[80];

  printf("Insira seu nome: ");
  scanf("%s*c", player); 

  // Primeiro round: A x B
  // --------------------------------------------------

  printf("[ROUND %s x B]\n", player);
  printf("Jogador %s escolha entre par(0) ou impar(1): ", player);
  int aChoice = getNumber();

  printChoice(player, aChoice, "B");

  printf("Jogador %s escolha um número entre 0 e 10: ", player);
  int aNumber = getNumber();

  int bNumber = getRandomNumber(10);

  printNumbers(player, aNumber, "B", bNumber);

  printResult(aNumber, bNumber);

  const char* abWinner = getWinner(player, aNumber, aChoice, "B", bNumber);

  printWinner(player, "B", abWinner);

  int roundOne = strcmp(player, abWinner);

  // Segundo round: C x D
  // --------------------------------------------------

  printf("[ROUND C x D]\n");
  printf("Jogador C escolha entre par(0) ou impar(1): ");
  int cChoice = getRandomNumber(1);
  printf("%d\n", cChoice);

  printChoice("C", cChoice, "D");

  printf("Jogador C escolha um número entre 0 e 10: ");
  int cNumber = getRandomNumber(10);
  printf("%d\n", cNumber);

  int dNumber = getRandomNumber(10);

  printNumbers("C", cNumber, "D", dNumber);

  printResult(cNumber, dNumber);

  const char* cdWinner = getWinner("C", cNumber, cChoice, "D", dNumber);

  printWinner("C", "D", cdWinner);

  // Round final
  // --------------------------------------------------

  printf("[ROUND %s x %s]\n", abWinner, cdWinner);

  printf("Jogador %s escolha entre par(0) ou impar(1): ", abWinner);

  int fChoice = (!roundOne) ? getNumber() : getRandomNumber(1);

  if(roundOne)
    printf("%d\n", fChoice);

  printChoice(abWinner, fChoice, cdWinner);

  printf("Jogador %s escolha um número entre 0 e 10: ", abWinner);

  int abNumber = (!roundOne) ? getNumber() : getRandomNumber(10);

  if(roundOne)
    printf("%d\n", abNumber);

  int cdNumber = getRandomNumber(10);

  printNumbers(abWinner, abNumber, cdWinner, cdNumber);

  printResult(abNumber, cdNumber);

  const char* winner = getWinner(abWinner, abNumber, fChoice, cdWinner, cdNumber);

  printWinner(abWinner, cdWinner, winner);

  if(strcmp(player, winner) == 0) {
    printf("Você ganhou!\n");
  }else {
    printf("Você perdeu!\n");
  }

  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.