Sign in to follow this  
OneThreeThreeSeven

Codechef not accepting my code O_o

Recommended Posts

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

Share this post


Link to post
Share on other sites
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;

}

Share this post


Link to post
Share on other sites
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';
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this