【问题标题】:When submitting to Codeforeces output isn't the same as in the console提交到 Codeforeces 时的输出与控制台中的不同
【发布时间】:2021-07-27 09:31:57
【问题描述】:

我正在尝试解决这个问题:(https://codeforces.com/contest/1363/problem/A),并且在我的控制台中,当我给它第一个示例的输入时,它会输出正确的答案。但是在提交代码时它说我的输出是错误的,我不知道是什么问题。 这是我的代码:

#include <iostream>
#include <vector>
#include <math.h>

#define endl '\n'
using namespace std;

bool solve(int sum, int n, int x, int i, vector<int> v)
{
    if(x == 0)
        return sum % 2 != 0;

    bool c1, c2;

    c1 = solve(sum + v[i], n, x - 1, i + 1, v);
    if(i == n - 1 - x)
        c2 = solve(sum, n, x, i + 1, v);

    return c1 || c2;
}

int main()
{
    fast;

    int t;
    cin >> t;
    while(t--)
    {
        int n, x;
        cin >> n >> x;
        vector<int> v(n);

        for(int i = 0; i < n; i++)
            cin >> v[i];

        if(solve(0, n, x, 0, v))
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
}

【问题讨论】:

  • #define endl '\n' 是邪恶的。 endl\n 不同。与using namespace std; 一起,此类宏会造成混淆。请不要这样做
  • 好的,感谢您的帮助
  • 您似乎也使用了另一个名为 fast 的精美宏,但缺少定义

标签: c++ output


【解决方案1】:

行为可能不同,因为您的代码具有未定义的行为 - 函数 solve 中的变量 c2 可以未初始化使用。

【讨论】:

  • @Mohammedaldubaisi 当程序结果在两个执行环境中不同时,它很可能具有某种未定义行为 - 未初始化的变量/数组访问越界等。
猜你喜欢
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
相关资源
最近更新 更多