By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Count Beers - C# Task

+4 votes

Nakov is keen football fаn and he is passionate about the upcoming FIFA World Cup tournament. A month before the tournament Nakov started his preparations with a few stacks of beer and a few single beer bottles. You are given a list of Nakov's stacks and Nakov's single beers. Help him to count how many stacks of beer and how many additional single bottles he has for the tournament. Assume that a stack of beer holds 20 beers.


The input comes from the console as list of beers and stacks of beer, each at a single line, ending with the text "End" at the last line. Each line (except the last) come in format "count measure" where count is a positive integer and measure is either "stacks" or "beers" (see the examples below).

The input data will always be valid and in the format described. There is no need to check it explicitly.


Print at the console the total number of stacks and the number of single beers that Nakov has in the following format: "x stacks + y beers", where x is an integer number and y is an integer number, less than 20.


  • The count will be an integer number in the range [1…99].
  • The measure is either "stacks" or "beers".
  • The number of input lines will be in the range [1..999].
  • Time limit: 0.3 sec. Memory limit: 16 MB.


count beers examples

asked in C# category by user andrew
edited by user golearnweb

1 Answer

+2 votes
Best answer

Here's my solution - In this task you must think more of how to input the data with strings and arrays - and they know which element represents which data; The logic is not hard... :-)

using System;

class Beers
    static void Main()
        string line = Console.ReadLine();//INPUT STRING

        int beerCount = 0;//THE COUNTER FOR THE NUMBER OF BEERS
        while (line != "End")
            string[] data = line.Split(' ');//ARRAY MADE OF THE INPUT STRINGS SPLIT BY EMPTY SPACE
            int count = int.Parse(data[0]);//1ST ELEMENT OF THE ARRAY
            string type = data[1];//SECOND ELEMENT OF THE ARRAY

            if (type == "beers")
                beerCount += count;
            else if (type == "stacks")
                beerCount += count * 20;

            line = Console.ReadLine();//THE NEXT INPUT STRING MUST BE AT THE END!
        int totalStacks = beerCount / 20;
        int leftBeers = beerCount % 20;
        Console.WriteLine("{0} stacks + {1} beers", totalStacks, leftBeers);


answered by user ak47seo
edited by user golearnweb
thank you my friend! Nice one! :-0)0