2

请看下面的代码

#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

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

4

1 回答 1

4

如果有可能到达那里,千万不要在没有条件的情况下离开if,else if块。elseelse条件添加到您的代码中。

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
}
于 2013-02-13T05:52:43.067 回答