This is my code. It runs correctly with custom example inputs, but fails when I submit it. Also I don't know how to add constraints as given in question. The link to the question is : https://www.codechef.com/submit/HS08TEST.
#include <iostream>
#include<iomanip>
using namespace std;
int main() {
// your code goes here
int r;
cin >> r;
float balance;
cin >> balance;
float amount;
if (r%5==0 && r<balance ){
amount = float(balance - r - 0.5);
cout << fixed;
cout << setprecision(2) << amount;
}
else {
cout<< fixed;
cout << setprecision(2) << balance;
}
return 0;
}
CodePudding user response:
The account should have enough balance for bank charges.
#include <iostream>
#include<iomanip>
using namespace std;
int main() {
// your code goes here
int r;
cin >> r;
float balance;
cin >> balance;
float amount;
if (r%5==0 && (r 0.5) <= balance ){
amount = float(balance - r - 0.5);
cout << fixed;
cout << setprecision(2) << amount;
}
else {
cout<< fixed;
cout << setprecision(2) << balance;
}
return 0;
}
CodePudding user response:
Consider constraint: 120 120.49 Your code outputs -0.01 for this while you can see answer should be 120.49 as when bank will charge you 0.5 for transaction, your balance become insufficient. Try using this in the if condition:
if (r%5==0 && r 0.5<=balance)
I think you got where the code is missing.