gpt4 book ai didi

c-段错误 :11

转载 作者:行者123 更新时间:2023-11-30 19:52:08 24 4
gpt4 key购买 nike

该代码用于查找数组 a 中存在的所有可能的数字组合,这些组合加起来等于给定数字 n。我在这段代码中遇到段错误错误是段错误:11。请帮忙。

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

void comb(long long int n,int a[],int k);
long int ct=0;

int main() {
long long int n;
scanf("%lld",&n);
int k;
scanf("%d",&k);
int a[k];

for(int i=0;i<k;i++)
scanf("%d",&a[i]);

comb(n,a,k);
ct=ct*2;
printf("%ld",ct);
return 0;
}

void comb(long long int n,int a[],int k)
{
if(n==0)
{
ct++;
return;
}
else
if(n<0)
return;
else
{
for(int j=0;j<k;j++)
{
comb(n-a[j],a,k);
}
}
}

最佳答案

问题是 scanf安全。使用fgets并添加一些错误检查

例如,如果用户在 scanf("%lld", &n); 上键入字母,您的程序会因段错误而崩溃。

关于c-段错误 :11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26044621/

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