Voting

From programming_contest
Jump to navigation Jump to search

Introduction

This problem asks you to take a series of inputs, representing votes, and decide the result of the voting. The possible inputs are:

  • Y: the voter voted "yes"
  • N: the voter voted "no"
  • A: the voter was absent
  • P: the voter was present but did not vote (or voted blank, works the same)

Solution

The question is really straight forward. All you have to do is to count the number of occurrences of each letter (or vote), and count the total number of votes. Then, follow the algorithm:

  • 1) If 2*A >= total: "need quorum", for there are not enough people in the voting.
  • 2) Else If Y == N: "tie", for there is the same number of votes "yes" and votes "no.
  • 3) Else If Y > N: "yes", for there are more votes "yes" than votes "no".
  • 4) Else If Y < N: "no", for there are more votes "no" than votes "yes".

Code

Solution - C++

#include <stdio.h>

int main() {
    char txt[100];

    while (scanf(" %s", txt) && txt[0] != '#') {
        int y, n, p, a, t;
        y = n = p = a = t = 0;

        for (int i = 0; txt[i] != '\0'; i++) {
            if (txt[i] == 'Y')
                y++;
            if (txt[i] == 'N')
                n++;
            if (txt[i] == 'P')
                p++;
            if (txt[i] == 'A')
                a++;
            t++;
        }

        if (a*2 >= t)
            printf ("need quorum\n");
        else if (y == n)
            printf ("tie\n");
        else if (y > n)
            printf ("yes\n");
        else
            printf ("no\n");
    }

    return 0;
}