【问题标题】:I have a problem wth a Codechef ATM question, my code works fine with custom inputs which are in the example, but fails when submitted我对 Codechef ATM 问题有疑问,我的代码适用于示例中的自定义输入,但提交时失败
【发布时间】:2022-01-24 04:40:08
【问题描述】:

这是我的代码。它使用自定义示例输入正确运行,但在我提交时失败。 另外我不知道如何添加问题中给出的约束。 问题的链接是:https://www.codechef.com/submit/HS08TEST

#include <iostream>
#include<iomanip>
using namespace std;

int main() {
    // your code goes here
    int r;
    cin >> r;
    float balance;
    cin >> balance;
    
    float amount;
    
    if (r%5==0 && r<balance ){
        amount = float(balance - r - 0.5);
        cout << fixed;
        cout << setprecision(2) << amount;
    }
    else {
        cout<< fixed;
        cout << setprecision(2) << balance;
    }
    return 0;
}

【问题讨论】:

标签: c++


【解决方案1】:

帐户应有足够的余额支付银行费用。

#include <iostream>
#include<iomanip>
using namespace std;

int main() {
    // your code goes here
    int r;
    cin >> r;
    float balance;
    cin >> balance;
    
    float amount;
    
    if (r%5==0 && (r+0.5) <= balance ){
        amount = float(balance - r - 0.5);
        cout << fixed;
        cout << setprecision(2) << amount;
    }
    else {
        cout<< fixed;
        cout << setprecision(2) << balance;
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    考虑约束:120 120.49 您的代码为此输出 -0.01,而您可以看到答案应该是 120.49,因为当银行将向您收取 0.5 的交易费用时,您的余额将变得不足。 尝试在 if 条件中使用它:

    if (r%5==0 && r+0.5<=balance)
    

    我认为您找到了缺少代码的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 2020-07-28
      • 2020-03-25
      • 2019-06-24
      • 2020-07-28
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      相关资源
      最近更新 更多