Not sure what is wrong with this code. These are the input/output that I get
12 --> 13
13 --> 16
When I submit it, the system tell wrong answer. Any suggestions will be appreciated.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
public class ByteLandianColdCoin {
public static HashMap map = new HashMap();
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s= br.readLine()) != null && s.length()!=0)
{
double i = Double.parseDouble(s.trim());
System.out.println(new Double(change(i, "")).longValue());
}
} catch (Exception e) {
System.err.println("Error:" + e.getMessage());
}
}
public static double change(double coin, String ind)
{
//System.out.println(ind+" coin: "+coin);
Double ddollar = ((Double)map.get(coin));
if(ddollar!= null)
{
//System.out.println(ind+" dollar:"+ddollar.doubleValue());
return ddollar.intValue();
}
ind = ind+" * ";
//System.out.println(ind+" in change -"+coin);
if(coin <12)
{
map.put(coin, coin);
//System.out.println(ind+map);
return coin;
}
double dollar = change(Math.ceil(coin/2), ind)+change(Math.ceil(coin/3), ind)+change(Math.ceil(coin/4), ind);
dollar = coin>dollar?coin:dollar;
map.put(coin, dollar);
//System.out.println(ind+map);
//System.out.println(ind+" dollar:"+dollar);
return dollar;
}
}