Voting: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Rm252
Created page with "= 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"..."
 
imported>Kmk21
No edit summary
 
Line 56: Line 56:
[[Category:mcpc2010]]
[[Category:mcpc2010]]
[[Category:ICPC Problems]]
[[Category:ICPC Problems]]
[[Category:Algorithm Easy]]
[[Category:Algorithm Trivial]]
[[Category:Implementation Easy]]
[[Category:Implementation Trivial]]

Latest revision as of 19:24, 13 January 2016

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;
}