Input
The input text consists number of sets of problems. The first line of a set is of the form “COST a b”.
For that set:
- a and b are both integers, 0 ≤ a, b ≤ 1000
- a 0 binary digit costs a dollars
- a 1 binary digit costs b dollars.
The first line is followed by one or more lines each consisting of a single integer n.
- 0 ≤ n ≤ 2,000,000
- n indicates a house number, expressed as a standard decimal number.
A single # on a line indicates the end of input.
Output
Each set of output data must begin with a single output line showing consisting of the word “Set”, followed by a space (“ ”), and the current set number (counted from 1). This is followed by the cost of the binary digits for each house number, each cost being displayed as a decimal number on a separate line.
Samples Input
COST 1 1
1
34
15
COST 1 10
1
34
15
COST 10 1
1
34
15
COST 0 5
1
16
#
Samples Output
Set 1
1
6
4
Set 2
10
24
40
Set 3
1
42
4
Set 4
5
5
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 | import java.util.*; public class Q4 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String line = System.getProperty("line.separator"); scan.useDelimiter(line); String ans = ""; int a = 0, b = 0, no = 0, ct = 1; String op = scan.next(); while (!op.equalsIgnoreCase("#")) { if (op.contains("COST")) { ans = ans + "Set " + ct + "\n"; String get[] = op.split(" "); a = Integer.parseInt(get[1]); b = Integer.parseInt(get[2]); ct++; } else { no = Integer.parseInt(op); String c = Integer.toBinaryString(no); int z = 0, s = 0; for (int x = 0; x < c.length(); x++) { if (c.charAt(x) == '1') s++; else z++; } int total = (z * a) + (s * b); ans = ans + total + "\n"; } op = scan.next(); } System.out.println(ans); } } |
No comments:
Post a Comment