#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int wd;
double bal;
cin >> wd;
cin >> bal;
if ( wd % 5 != 0 )
{
cout << setprecision (2) << fixed << bal;
}
else if ( wd == bal )
{
cout << setprecision (2) << fixed << bal;
}
else if ( wd > bal )
{
cout << setprecision (2) << fixed << bal;
}
else if ( wd < bal )
{
wd = bal - wd;
wd = wd - 0.50;
cout << setprecision (2) << fixed << (double) wd + 0.50;
}
return 0;
}
Codechef not accepting my code O_o
I'm doing this problem here
http://www.codechef.com/problems/HS08TEST
It shows as a wrong answer on code chef...but I have no idea why.
You're doing the wrong comparisons.
What if the balance is $10.49 and he tries to withdraw $10?
Your code will give the wrong result.
What if the balance is $10.49 and he tries to withdraw $10?
Your code will give the wrong result.
You need only 3 if statements. One for successful attempt, one for not being % 5,
and the last one for insufficient funds.
So here is the skeleton :
and the last one for insufficient funds.
So here is the skeleton :
unsigned int get = 0;float balance = 0.0f;while( cin >> get >> balance ){ if not % 5 print balance break; else if get > balance - 0.5f print balance break; (what Storyyeller was talking about) else if sucessful print balance - get - 0.5 with fixed 2-point precision break;}
Or no if statements at all:
#include <iostream>#include <iomanip>int main( int, char*[] ) { int withdraw; float balance; std::cout << std::fixed << std::setprecision( 2 ) << ( ( ( std::cin >> withdraw >> balance ) && !(withdraw % 5) && withdraw + 0.5 <= balance ) ? balance - withdraw - 0.5 : balance ) << '\n';}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement