lundi 26 septembre 2016

Code Chef: Code Not Correct? Unexpected Behaviour

Recently I've started doing Code Chef challenges to practice programming in C and C++ when I came across the ATM challenge. The challenge (which I expected to be relatively easy) was to create a program that withdraws money from a bank account if the withdrawal amount is a multiple of 5 and if the withdraw > balance. If the transaction is accepted it also charges a fee of 0.5, else it just reprints the bank data.

The link to the challenge is here: http://ift.tt/2cxGSUt

The program runs perfectly fine for me on my computer but for some reason it is wrong.

Here is my program:

#include <iostream>
#include <cstdio>
#include <iomanip>

using std::cout; using std::cin; using std::endl;

class Bank_data {
private:
  double balance;
public:
  void withdraw (int amount) {
    if (amount > balance - 0.5 || amount % 5 != 0 || amount >= balance)
      printf("%.2f\n", balance);
    else
      printf("%.2f\n", balance - amount - 0.5);
  }
  void setBalance (int amount) {
    balance = amount;
  }
};

int main () {

  Bank_data bd;
  int withdraw, init_val;

  cin >> std::fixed;
  cin >> std::setprecision(2) >> withdraw >> init_val;

  bd.setBalance (init_val);
  bd.withdraw (withdraw);
  return 0;
}

Sample io:

I1: 5 500

O1: 494.50

I2: 600 500

O2: 500

I3: 4 500

O3: 500

Aucun commentaire:

Enregistrer un commentaire