0

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;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 2
    Be wary of learning C++ from coding sites. They often promote bad habits. While you may be given that the first two inputs would be an `int` and a `float`, you would never fail to check the stream-state following `std::cin` of a numeric value. Additionally see [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Dec 23 '21 at 05:49

2 Answers2

1

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;
}
gofun
  • 49
  • 3
1

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.