gpt4 book ai didi

algorithm - 提交后 Geeksforgeeks 将我的代码显示为运行时错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:53:54 27 4
gpt4 key购买 nike

这是我的代码

#include <bits/stdc++.h>
using namespace std;

int srch(vector<int> arr, int ln, int fn)
{
for (int i = 1; i <= ln; i++)
{
if (arr[i] == fn)
return i;
}
return -1;
}

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k;
scanf("%d%d", &n, &k);
vector<int> a(n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf("%d\n", srch(a, n, k));
}
return 0;
}

我不明白问题出在哪里。这是问题链接 Click here .请帮我解决这个问题。我不明白为什么 geeksforgeeks 显示此代码的运行时错误。

最佳答案

请注意,您的循环使用向量的第 n 个条目,但是

vector< int> a(n);

索引从0到n-1

Notice that the first element has a position of 0 (not 1).

关于algorithm - 提交后 Geeksforgeeks 将我的代码显示为运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51645878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com