gpt4 book ai didi

c - 为什么它显示运行时错误?它没有给出任何输出?

转载 作者:行者123 更新时间:2023-11-30 20:04:35 26 4
gpt4 key购买 nike

/此代码用于将数组旋转 n 次 k 次 并在索引z处输出数组元素q次/我的问题是它显示运行时错误为什么会发生这样的情况。这个问题实际上来自黑客级别,它的名称是循环数组旋转在算法的实现部分。这段代码有什么问题吗?

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

int main() {

int n,k,q;
int a[n];
scanf("%d%d%d",&n,&k,&q);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int j=0;j<k;j++)/*this is for rotating the array*/
{
int y=a[n-1];
for(int x=n-2;x>=0;x--)
a[x+1]=a[x];
a[0]=y;
}
for(int b=0;b<q;b++)
{
int z;
scanf("%d",&z);
printf("%d\n",a[z]);
}
return 0;
}

最佳答案

问题:

int n,k,q;
int a[n];

在设置 n 的值之前,您将创建一个大小为 n 的数组。

用途:

int n,k,q;

// Read a value into n first
if ( scanf("%d%d%d",&n,&k,&q) != 3 )
{
// Deal with error
return 1;
}

// Then define the array.
int a[n];

关于c - 为什么它显示运行时错误?它没有给出任何输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39211572/

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