[Solved] Problem solving in C# Task - Piggy Bank

  

3
Topic starter

Anastas wants to buy himself a tank to drive around the streets of Sofia (he’s a fan of the classic game Carmageddon). He’s saving up and he needs your help to keep track of his progress.

Every day in a given month he saves up 2 leva and puts them in his piggy bank. Unless there is a party that day – he needs 5 leva to buy vodka on a party day, so he takes them out of the piggy bank.

You will be given the tank’s price and the number of party days in a month, each on a separate line. Assume each month has 30 days and each year has 12 months. Calculate how many years and months Anastas will need in order to save enough to buy his very own tank and print the result on the console in the format “X years, Y months”. In case he isn’t saving up at all and is wasting money on cheap alcohol instead, print “never”.

Note that if, for example, Anastas needs 3.1 months, you need to round that up – so you have to print “0 years 4 months”. The years and months should be integer numbers. Check out the examples to understand your task better.

Input

The input will be read from the console. The input consists of exactly two lines:

  • On the first line you will be given an integer – the price of the tank.
  • On the second line you will be given the number of party days in a month.

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

Output

The output should be printed on the console.

  • On the only output line print the number of years and months Anastas will need to save enough money in the format “X years, Y months”, or print “never” in case he’s actually wasting money each month.

Constraints

  • The price of the tank will be an integer in the range [1 … 2 000 000 000].
  • The number of party days will be an integer in the range [0 … 30].
  • Allowed working time: 0.2 seconds. Allowed memory: 16 MB.

Examples

piggybank c sharp task

2 Answers
3

This one in my humble opinion is more accurate as in your's solution (eiorgrt) - if you have 12 months - THEY WILL NOT CONVERT IN 1 YEAR - and the var year will be less in 1 and the months will be shown as 12 months... Mine solution however is rounding up 12 months to 1 year 🙂

using System;
 
class PiggyBank
{
    static void Main()
    {
        int price = int.Parse(Console.ReadLine());
        int partyDays = int.Parse(Console.ReadLine());
        if (partyDays > 8)
        {
            Console.WriteLine("never");
        }
        else
        {
            int normalDays = 30 - partyDays;
 
            int monthlySavings = normalDays * 2;
            int monthlyExpenses = partyDays * 5;
            int monthlyBalance = monthlySavings - monthlyExpenses;
 
            double totalMonthsRequired = (double)price / monthlyBalance;
            int result = (int)Math.Ceiling(totalMonthsRequired);
            int years = result / 12;
            int months = result % 12;
            Console.WriteLine("{0} years, {1} months", years, months);
        }
    }
}
2

This is my solution to the task:

using System;
 
class PiggyBankTank
{
    static void Main()
    {
        decimal priceOfTank = decimal.Parse(Console.ReadLine());//the price of the tank
        decimal partyDays = decimal.Parse(Console.ReadLine());//party days in a month * 5
 
        decimal daysInMonth = 30;
 
        decimal realSavingMoneyDays = daysInMonth - partyDays;
        decimal realSavingMoney = realSavingMoneyDays * 2;
 
        decimal wasteMoney = partyDays * 5;
        realSavingMoney -= wasteMoney;//real saving money per month
 
        decimal yearsForTank = (priceOfTank / realSavingMoney) / 12;//years required for buying a tank
        decimal monthsForTank = (priceOfTank / realSavingMoney) % 12;//months required for buying a tank
 
        if (yearsForTank < 0 || monthsForTank < 0)
        {
            Console.WriteLine("never");
        }
 
        else
        {
            Console.WriteLine("{0} years, {1} months", (int)(yearsForTank), (int)Math.Ceiling(monthsForTank));
        }
    }
}
Share: