ATM机编程挑战

时间:2013-02-13 05:39:32

标签: c++ visual-studio-2010

请查看以下代码

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double balance=0;
    int withdraw = 0;
    double const bankCharges = 0.5;


    cin >> withdraw >> balance;

    if(withdraw%5==0 && balance>(withdraw+bankCharges))
    {
        cout << fixed << setprecision(2) << ((balance)-(withdraw+bankCharges)) << endl;
    }
    else if(withdraw%5!=0 || withdraw>balance)
    {
        cout << fixed <<setprecision(2) << balance << endl;
    }


    return 0;


}

创建上述代码是为了解决以下挑战

http://www.codechef.com/problems/HS08TEST

如您所见,我的代码提供了正确的答案。但测试引擎说“错误答案”!!!!!为什么?请帮忙!

1 个答案:

答案 0 :(得分:4)

如果甚至有可能到达那里,切勿在没有if条件的情况下离开else ifelse区块。将else条件添加到您的代码中。

if(withdraw%5==0 && balance>(withdraw+bankCharges))
{
    cout << fixed << setprecision(2) << ((balance)-(withdraw+bankCharges)) << endl;
}
else if(withdraw%5!=0 || withdraw>balance)
{
    cout << fixed <<setprecision(2) << balance << endl;
}
else
{
   //Do something
}
相关问题