0
[Solved][Spoiler] Paint cost?
Don't read this question or it's answers if you don't want the solution So I'm doing the paint cost coach and this code fails 2 of the test cases. Can somebody give me a hint? What's wrong? https://code.sololearn.com/c9V8ShOswjsh/?ref=app
17 ответов
+ 6
Must be some floating point precision stuff leading to errors there, I guess. It works if you a) divide cost by 10 instead of multiplying by 0.1 and b) cast the obtained value from rounding up to int when printing it, but don't ask me why, lol.
+ 3
Another approach that I sometimes use is to replace double with float. I tried it in your code and saw consistently correct results. In similar fashion I have made improvements on other programs in SoloLearn. I had reasoned that using float filtered out unwanted smaller extra bits that double can store. (In technical terms, float has a larger epsilon than double has). However, I am not so sure that is a sufficient explanation here. I think this is worth further investigation and will see if I can find time to analyze it further.
EDIT: Using 10 for input I found that the double precision calculation ended with its smallest bit set. That made it high by about 4.94x10^-324, which was enough to push the ceil up from 99 to 100. I did not find any wrong calculation. It is the nature of decimal-to-binary representation.
+ 2
Shadow My guess is it works better because 0.10 itself can't be stored without some error.
Then, when you multiply with it, that error gets even bigger.
10.0 can be stored without any error I believe.
Daniel Cooper
When you're working with money, in the real world, it's usually a better idea to work with integrals instead.
Instead of storing 3.56 in a float, you'd store 356 in an int ( or long long ) for 2 decimal precision or 3560 for 3.
That way you're not affected by floating point errors.
+ 2
// Paint costs in c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int numberOfcolor,total_cost;
double cost,tax;
cin>>numberOfcolor;
cost=(numberOfcolor*5)+40;
tax=cost/10;
total_cost=round(tax)+cost;
cout<<total_cost;
}
+ 2
Naman Chhabra you will get the most value out of Code Coach if you solve it yourself. As for me, floats in C and had no problems passing the tests.
+ 2
Naman Chhabra I think it works best if you separately calculate and store the subtotal, tax, total, and then find the ceiling of the total to round up to dollars. Just now I went back to my code and tried combining some steps to see if that was why some attempts fail, and this time some tests failed. Of course, restoring the code worked again.
+ 2
Naman Chhabra you are welcome!
+ 2
#include <iostream>
#include<cmath>
using namespace std;
int main() {
int color,tax,total,final;
cin>>color;
final=40 +color*5;
tax=floor(final/10);
if(tax>1000 && tax<10000){
tax++;
}
total =final +tax;
cout<<total;
return 0;
}
//THIS WORKED FOR ME GUYS
+ 1
Dennis, I also had some ideas like that, thanks for clarifying. Do you also happen to have any thoughts on why the result needs to be casted to int? For example, casting the return value of std::ceil(), which is supposed to be an integer value - although returned as a float - and then storing the result in a double variable also provokes an error for me, although the final output that is being printed is identical to when you cast the variable itself to int when printing it.
It would be really interesting to know how the solutions are evaluated and compared.
+ 1
Shadow
I did a quick test using both methods and comparing the results, but there were no differences.
So I think this is just a sololearn issue.
+ 1
Thanks to everyone who answered. I ended up doing what Shadow said and it worked. Why did it work Shadow?
Anyway, I'll probably do some research to figure out the weird behavior.
+ 1
Daniel Cooper In case you did not follow our conversation, I can't exactly explain it either, especially without knowing how they fetch the solution and evaluate it to yours. As stated by Dennis, some issues from the side of SoloLearn should play a role here as well.
Dennis Thanks for taking your time checking it out anyway. Hopefully it might be resolved in the future.
+ 1
Brian My code is fail two test codes i am unable to find the mistake
+ 1
Thank you so much brother🙏
0
Can anybody give the correct solution of paint cost problem??
0
This works for me
I know it is very old Post, after many years I back to SL a few days back.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int noOfPaint;
cin>>noOfPaint;
cout<< int(round(((noOfPaint * 5) + 40) * 1.1));
return 0;
}
0
This was a beginner challenge marked "easy" and meant to be solved using beginner tools. Using things like #include <cmath>, round and ceil had not been taught yet at the "easy" level so the expectation is probably to use what you *have* been taught to solve the challenge.
#include <iostream>
using namespace std;
int main() {
int colors ;
cin >> colors ;
float preTax(40 + (colors*5)) ;
float taxExp(preTax+preTax/10+.99);
int roundUp(taxExp);
cout << roundUp ;
return 0;
}
https://code.sololearn.com/c8Pz0487WIIQ/?ref=app