c++ - ATM 机编程挑战

标签 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

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

最佳答案

如果有可能到达那里,就永远不要在没有 else 条件的情况下离开 ifelse if block 。将 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
}

关于c++ - ATM 机编程挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14847102/

相关文章:

c++ - 如何修复/重新设计模板递归依赖

visual-studio-2010 - VS/TFS 2010 DIFF 选项在哪里?

visual-studio-2010 - opencv 2.4.2 Visual Studio 2010 的配置问题?

c++ - 无法在 C# 中加载 64 位 dll 错误

c++ - 有什么方法可以在Visual Studio 2010的调试器中搜索变量值?

c# - 出版物(网络): Object reference not set to an instance of an object when trying to publish a web site

c++ - 即使使用 std::cin.ignore() 后,std::cin 也会被跳过

c++ - 迭代器应该读取一个完整的数字,但它只读取第一个数字

c++ - 在 C++ 中没有静态构造函数的理由是什么?

c++ - Qt:从固定数量的字节到整数