Uri/beecrowd problem no - 1131 solution in C

#include <stdio.h>

int main() {
    int inter, gremio, grenais = 0;
    int interWins = 0, gremioWins = 0, draws = 0;
    int option = 1;

    while (option == 1) {
        scanf("%d %d", &inter, &gremio);
        grenais++;

        if (inter > gremio) {
            interWins++;
        } else if (inter < gremio) {
            gremioWins++;
        } else {
            draws++;
        }

        printf("Novo grenal (1-sim 2-nao)\n");
        scanf("%d", &option);
    }

    printf("%d grenais\n", grenais);
    printf("Inter:%d\n", interWins);
    printf("Gremio:%d\n", gremioWins);
    printf("Empates:%d\n", draws);

    if (interWins > gremioWins) {
        printf("Inter venceu mais\n");
    } else if (interWins < gremioWins) {
        printf("Gremio venceu mais\n");
    } else {
        printf("Nao houve vencedor\n");
    }

    return 0;
}

Explanation:

The given code is a solution to the URI Beecrowd problem number 1131 in C. This problem is about recording the results of several Gre-Nais matches (a football rivalry in Brazil) and displaying the statistics of the matches.

  • The code starts by including the necessary header file stdio.h for input and output operations.
  • The main() function is defined, which is the entry point of the program.
  • Several variables are declared:
  • inter and gremio to store the number of goals scored by Inter and Gremio in each match.
  • grenais to store the total number of matches played.
  • interWins, gremioWins, and draws to store the number of wins for Inter, Gremio, and draws respectively.
  • option to store the user's choice for continuing the program or not.
  • A while loop is used to repeatedly input the results of each match until the user chooses to stop.
  • Inside the loop:
  • The user is prompted to enter the number of goals scored by Inter and Gremio using the scanf() function.
  • The total number of matches (grenais) is incremented.
  • Using an if-else statement, the code determines the winner of the match or if it resulted in a draw, and updates the respective counters (interWins, gremioWins, draws) accordingly.
  • The user is prompted to enter their choice for continuing the program or not (1 for yes, 2 for no).
  • After the loop ends:
  • The statistics of the matches are displayed using printf() statements.
  • The winner of the overall matches is determined by comparing the values of interWins and gremioWins, and the result is displayed.
  • Finally, the main() function returns 0 to indicate successful program execution.

The code uses basic input/output operations (scanf() and printf()) to interact with the user and display the results. It also utilizes loops, conditionals, and variables to keep track of the match results and calculate the statistics.