gpt4 book ai didi

c - "file_name.exe has stopped working"

转载 作者:行者123 更新时间:2023-11-30 21:39:14 25 4
gpt4 key购买 nike

这个问题来自spoj。请参阅this供提问。正如您所看到的,我的逻辑很简单。如果我将动态形式的数组大小更改为静态数组大小,则代码将执行并请求整数,并无限期地继续执行,同时它应该运行测试用例的数量。但是,如果我保持动态,我就会不断收到此持续错误:file_name.exe 已停止工作。

也请对我的代码发表评论。

#include <stdio.h>
#include <conio.h>

int main()
{
int n, i, count, k, tcs, j, l, m;
scanf("%d", tcs);
int fact[tcs];
int arr[tcs];
for (j = 0; j < tcs; j++) {
scanf("%d", &arr[j]);
}

int calc_fact(n) {
if (n == 1)
return 1;
else
return n * calc_fact(n - 1);
}

j = 0;
m = 0;
while (m < tcs && j < tcs) {
fact[m] = calc_fact(arr[j]);
m++;
j++;
}

m = 0;
for (l = 0; l < tcs; l++) {
i = 1;
count = 0;
while (i <= fact[m]) {
if ((fact[m]) % i == 0)
count++;
i++;
}
m++;
k = (count) % ((10 ^ 9) + 7);
printf("\n%d", k);
}
return 0;
}

最佳答案

假设您使用的是 C++,则需要在代码中更改一些内容:

  1. scanf("%d", tcs); 应该是 scanf("%d", &tcs); 这个错误是你的程序崩溃的原因,因为tcs 没有正确的预期值。
  2. 函数 int calc_fact(n) 应位于原型(prototype)为 int calc_fact(int n) 的 main() 函数之外。由于这个错误,您的代码首先将无法编译(当您说静态声明数组时您的代码符合要求时,您确定吗?

请参阅下面修改后的代码:

#include <stdio.h>
//#include <conio.h> //You actually don't need it.

int calc_fact(int n)
{
if(n==1)
return 1;
else
return n*calc_fact(n-1);
}

int main()
{
int n, i, count, k, tcs, j, l, m;
scanf("%d", &tcs);
int fact[tcs];
int arr[tcs];
for(j=0; j<tcs; j++)
{
scanf("%d", &arr[j]);
}

j=0;
m=0;
while(m<tcs && j<tcs)
{
fact[m]=calc_fact(arr[j]);
m++;
j++;
}


m=0;
for(l=0; l<tcs; l++)
{
i=1;
count=0;
while(i<=fact[m])
{
if((fact[m])%i==0)
count++;
i++;
}
m++;
k=(count)%((10^9)+7);
printf("%d\n", k); //and not printf("\n%d", k);

}

return 0;
}

工作代码here .

关于c - "file_name.exe has stopped working",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37693297/

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