请看下面的代码

#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

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

最佳答案

如果极有可能到达ifelse if块,则永远不要离开else条件。将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
}

07-26 00:04