KAN13G - Editorial

PROBLEM LINK:

Practice
Contest

Author: Dr. M Kaykobad
Tester: Jingbo Shang
Editorialist: Jingbo Shang

DIFFICULTY:

Easy

PREREQUISITES:

Greedy

PROBLEM:

Given a coin system, with 1 and 0.01, greedily determine the change plan for some given values.

EXPLANATION:

Because we need to use as many larger coin as possible, we first sort the coins in decreasing order. And then, greedily select coins as many as possible.

It will be better if you use integers rather than decimals by multiplying all input values with 100.

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.

here in this problem why we need to add some fractional value while converting double into int. it has caused me a lot of WA

`double d;
 cin>>d;
  int x=(int)(d*100);`


double d;
cin>>d;
int x=(int)(d*100+.005);

 both approach gives the same result on my machine but on submitting the first one gives WA.

Can anyone please tell why my code is giving WA???
http://www.codechef.com/viewsolution/3099344

You’d better use

int x = (int) (d * 100 + 1e-6)

add a small epsilon, not 0.005.

1 Like

the ‘val * 100’ may not work well as supposed. You can see my previous comments.

yeah i understand ur point …does we need to add epsilon whenever we convert a double to int