-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBytelandian Coins.java
More file actions
61 lines (50 loc) · 1.75 KB
/
Bytelandian Coins.java
File metadata and controls
61 lines (50 loc) · 1.75 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
In Byteland they have a very strange monetary system.
Each Bytelandian gold coin has an integer number written on it.
A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4.
But these numbers are all rounded down (the banks have to make a profit).
You can also sell Bytelandian coins for American dollars.
The exchange rate is 1:1, but you can not buy Bytelandian coins.
You have one bytelandian coin.
What is the maximum amount of American dollars you can get for it?
*/
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
// instantiate the solution map
public static Map<Integer, Integer> solutions = new HashMap<Integer, Integer>();
public static void main (String[] args) throws java.lang.Exception
{
// initialize a buffer
BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
// initialize storage for a line of text
String line;
int coin;
// while I'm getting input lines, iterate over them
while ((line=reader.readLine()) != null) {
// get the integer value of the line, and save it as a coin
coin = Integer.parseInt(line);
// optimize the coin, and print the new total
System.out.println(optimize(coin));
}
}
// optimize a coin
public static int optimize(int coin) {
// if it's not at least 12, return immediately
if (coin < 12) return coin;
// check if we already know this value, if we don't,
if (!solutions.containsKey(coin)) {
int total = 0;
// break it, and optimize the new coins
for ( int i=0; i < 3; i++ ){
total += optimize(coin / (i + 2));
}
solutions.put(coin, Math.max(total, coin));
}
// return the solution for coin
return solutions.get(coin);
}
}