Public Group

# Codechef not accepting my code O_o

This topic is 3194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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.

##### 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 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 on other sites
?: is just a more concise equivalent of if statements.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 12
• 12
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632654
• Total Posts
3007669
• ### Who's Online (See full list)

There are no registered users currently online

×