提交到 Codeforeces 时输出与控制台中的输出不同
When submitting to Codeforeces output isn't the same as in the console
我正在尝试解决这个问题:(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;
}
}
行为可能有所不同,因为您的代码具有未定义的行为 - 函数 solve
中的变量 c2
可以在未初始化的情况下使用。
我正在尝试解决这个问题:(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;
}
}
行为可能有所不同,因为您的代码具有未定义的行为 - 函数 solve
中的变量 c2
可以在未初始化的情况下使用。