Task
In the supermarket of electronics, if you believe in TV commercials, there is a system of discounts: from two purchased goods fully paid only the cost of a higher-value product, and the other is provided free of charge. What amount of money is enough to pay for the purchase of three goods, if the price of each is known.
Input data:
Three natural numbers [latex]a, b, c[/latex] are prices of three products [latex]\left(1 ≤ a, b, с ≤ 10000\right)[/latex].
Output data:
Purchase cost.
Tests
# | Input data | Output data |
---|---|---|
1 | 2 2 2 | 4 |
2 | 78 2 45 | 80 |
3 | 452 89 88 | 540 |
4 | 50 4 67 | 71 |
5 | 15 37 20 | 52 |
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.*; import java.lang.*; import java.io.*; import java.util.Scanner; class Main { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int s=0; // "s" used as an auxiliary variable to store the amount of money if (a>=b&&a>=c) { s=a; if (b>=c) { s+=c; } else { s+=b; } else if (b>=a&&b>=c) { s=b; if (a>=c) { s+=c; } else { s+=a; } else if (c>=a&&c>=b) { s=c; if (a>=b) { s+=b; } else { s+=a; } System.out.print(s); } } |
Solution of the problem
Algorithm: you will have to pay the highest price, so let’s find it at first and save it in the variable s. Next, you need to select the product for free receipt, which you put in a couple of the most expensive. To obtain the least amount of money, the remaining goods must be the cheapest.