gpt4 book ai didi

c - HackerRank 说 ~ stdout 上没有回应 ~ C

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:48 25 4
gpt4 key购买 nike

我在尝试解决 this 时遇到了这个问题关于 HackerRank 的问题。在 repl.it 上我的代码运行良好,但在他们的控制台上我遇到了这个问题。

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int t; //qtd de viagens
int m; //dinheiro para os sorvetes
int n; //qtd de sabores de sorvetes
int c[n+1]; //preço de cada sorvete
int r[t];
int s[t];

scanf("%d", &t);
for(int j = 0; j < t; j++){
scanf("%d", &m);
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &c[i]);
}
for (int i = 1; i < n; i++){
for(int k =i+1; k <= n; k++){
if (c[i]+c[k] == m){
r[j] = i;
s[j] = k;
}
}
}
}
for(int i = 0; i < t; i++){
printf("%d %d\n", *&r[i], *&s[i]);
}
return 0;
}

输入:

 2
4
5
1 4 5 3 2
4
4
2 2 4 3

repl.it 上的输出:

1 4
1 2

HackerRank 上的输出:

~ no response on stdout ~

它还会给我一个段错误消息。

最佳答案

这里有两个问题:

  1. 您正在使用 VLA,但并非所有编译器都支持它。我说的是:int c[n+1] 例如。将其更改为动态分配

  2. 在使用变量之前用值初始化变量(就像您对 nt 所做的那样)。

关于c - HackerRank 说 ~ stdout 上没有回应 ~ C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42777704/

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