Sunday, January 3, 2016

UVA 11340 : Newspaper

 Ah, this question led me to learn new things, strangely.

It is pretty easy to implement this one, but I was getting WA. And then when I went on uDebug, I found this link : http://www.redgreencode.com/solving-uva-11340-in-java/  and I thought may be character encoding/decoding would be the reason I am stuck.

So, I read this article followed by another good article : http://www.joelonsoftware.com/articles/Unicode.html. I really enjoyed reading both of these articles. It's always great to learn something you don't know of, and I certainly didn't knew of unicode and all the stuff.

But alas, I still got a WA even after doing all the implementation corrections as mentioned in the articles. Later, I found out that the reason I am getting WA is because after finding the answer in cents, I was doing something as I have shown in the code uva11340-wrongway.cpp

You see the stupidity I was doing?! Well I was mixing up the strings with floating point values, and that's why all those WAs.

Simply the following has had to be done, as shown in the code uva11340-rightway.cpp!!

cout.precision(2);
cout<<fixed;
cout<<ans/100<<"$"<<endl;;
string res;
// ans was the variable containg the sum in cents
if(ans%100 !=0)
res = to_string(ans/100) + "." + to_string(ans%100) + "$";
else
res = to_string(ans/100) + ".00$";
cout<<res;

See if you have done some silly mistake like that, it was a big lesson that I learned today.