Codechef not accepting my code O_o

Started by
3 comments, last by Storyyeller 14 years, 3 months ago
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.

#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;
}

Advertisement
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.
I trust exceptions about as far as I can throw them.
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 :
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;}
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
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';}
?: is just a more concise equivalent of if statements.
I trust exceptions about as far as I can throw them.

This topic is closed to new replies.

Advertisement