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

Q: Problem solving in C# Task - Currency Check

+3 votes

Te4o is a big Battlefield fan. He's been saving money for months to buy the new Battlefield Hardline game. However, he has five options to buy the game from.

The first one is a shady Russian site selling games in rubles (Russian currency).

 Another option is an American site selling games in dollars (American currency).

Te4o's third option is the official site of the game - selling games in euros (European Union currency).

The final 2 options are Bulgarian sites B and M. Both of them sell in leva (Bulgarian currency).

B offers a very special deal - 2 copies of the game for the price of one.

M sells games for normal prices.

Te4o is very bad with math and can't calculate the game prices in leva. But he wants to impress his girlfriend by showing her he bought the cheapest game.

Assume that Te4o has a girlfriend, all games are identical,

100 rubles are 3.5 leva (1 rubles = 0.035 lv.)

1 dollar is 1.5 leva

1 euro is 1.95 leva

if Te4o buys 2 special games from B he can sell one of them for exactly half of the money he paid for both.

Your task is to write a program that calculates the cheapest game.

Input

The input data should be read from the console. It consists of five input values, each at a separate line:

  • The number r – amount of rubles Te4o has to pay for the game at the Russian site.
  • The number d – amount of dollars Te4o has to pay for the game at the American site.
  • The number e – amount of euro Te4o has to pay for the game at the official site.
  • The number b – amount of leva Te4o has to pay for the special offer at B.
  • The number m – amount of leva Te4o has to pay for the game at M's site.

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

Output

  • The output data must be printed on the console. On the only output line you must print the cheapest game price rounded up (removed "up") to the second digit after the decimal mark.

Constraints

  • The numbers r, d, e, b, m are integer numbers in range [0... 4,294,967,295].
  • Allowed working time for your program: 0.1 seconds.
  • Allowed memory: 16 MB.

Examples:

task screenshot

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

2 Answers

+2 votes
 
Best answer

Here is my solution, although I am not sure about the proper usage of the Math.Min (the function of Min of the method Math) - which can get the smallest (lowest) number of 2 numbers (it can only compare 2 numbers).... If somebdy has better solution about comparing the numbers...

 

Here is the code:

using System;

class CurrencyCheck
{
    static void Main()
    {
        //prices before converting
        decimal rubles = decimal.Parse(Console.ReadLine());
        decimal dollars = decimal.Parse(Console.ReadLine());
        decimal euro = decimal.Parse(Console.ReadLine());
        decimal bg1 = decimal.Parse(Console.ReadLine());
        decimal bg2 = decimal.Parse(Console.ReadLine());

        //prices after converting
        decimal a = rubles * 0.035m;//rublesToLeva
        decimal b = dollars * 1.5m;//dollarsToLeva
        decimal c = euro * 1.95m;//euroToLeva
        decimal d = (bg1 / 2);//bg1ToLeva
        decimal e = bg2;

        //using Math.Min to compare 2 numbers and find out the lowest  

        decimal compareNumbers = Math.Min(a, b);
        decimal compareNumbers1 = Math.Min(c, d);
        decimal compareNumbers2 = Math.Min(compareNumbers1, compareNumbers);
        decimal compareNumbers3 = Math.Min(compareNumbers2,d);
        Console.WriteLine("{0:f2}", compareNumbers3);
    }
}

 

answered by user nikole
selected by user golearnweb
+1 vote

Here is another elegant solution with arrays and implementing one of the function of the arrays - Min - 24th line (highlighted one).

Also beware that I am using the using System.Linq; - in order to use the Min function of the arrays!
 

using System;
using System.Linq;

class CurrencyCheckTask
{
    static void Main()
    {
        decimal r = decimal.Parse(Console.ReadLine());
        decimal d = decimal.Parse(Console.ReadLine());
        decimal e = decimal.Parse(Console.ReadLine());
        decimal levaB = decimal.Parse(Console.ReadLine());
        decimal levaM = decimal.Parse(Console.ReadLine());

        r = r / 100 * 3.5m;
        d = d * 1.5m;
        e = e * 1.95m;
        levaB = levaB / 2M;

        decimal[] prices = 
        {
        r, d, e, levaB, levaM
        };

        decimal minPrice = prices.Min();
        Console.WriteLine("{0:f2}", minPrice);
    }
}
answered by user hues
edited by user golearnweb
...