代码厨师:代码不正确?意外行为

时间:2016-09-27 02:15:31

标签: c++ c++11

最近,当我遇到ATM挑战时,我开始接受Code Chef挑战,以便在C和C ++中练习编程。挑战(我预计相对容易)是创建一个程序,如果提款金额是5的倍数和withdraw > balance,则从银行账户中提取资金。如果交易被接受,它还收取0.5的费用,否则它只是重新打印银行数据。

挑战的链接在这里: https://www.codechef.com/problems/HS08TEST

该程序在我的计算机上运行得非常好,但由于某种原因它是错误的。

这是我的计划:

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

示例io:

  • I:5 500

  • O1:494.50

  • I2:600 500

  • O2:500.00

  • I3:4 500

  • O3:500.00

0 个答案:

没有答案
相关问题