C程序未运行

时间:2018-07-21 23:26:19

标签: c

我编写了一个代码来检查一个数字是否不足,该数字在C中可以正常运行。然后我尝试修改该代码以打印一个范围内的不足数字,它成功编译,但是它总是在运行时崩溃,这是代码

#include <stdio.h>

int main() {
int num,c=0,i,k,b;
int t_factors;
int range;
int factors[50];

printf("Enter the total number of figures you want to check \n");
scanf("%d",&range);
for(num=1;num<=range;num++)
{
for(i;i<=num;i++)
{
    if (num%i==0)
    {
        factors[i]=i;
        t_factors++;
    }
    else
    {
        printf("");
    }
    }
    for(k;k<=t_factors;k++)
    {
        c=c+factors[k];
    }
    b=2*num;
    if (c<b)
    printf("%d is deficient \n",num);
    else
    printf("%d is not deficient \n",num);
}
    return 0;
}

2 个答案:

答案 0 :(得分:3)

造成问题的原因很可能是i变量未初始化,并且在执行num % i时不确定会发生什么。

答案 1 :(得分:1)

您不确定自己的变量是否正确初始化-ickt_factors。您也没有正确加载数组factors。对代码进行的一组基本更改(使用C99“几乎在任何地方定义变量”功能)都会产生:

#include <stdio.h>

int main(void)
{
    int range;

    printf("Enter the total number of figures you want to check: ");
    if (scanf("%d", &range) != 1)
        return 1;

    for (int num = 1; num <= range; num++)
    {
        int t_factors = 0;
        int factors[50];
        for (int i = 1; i <= num; i++)
        {
            if (num % i == 0)
                factors[t_factors++] = i;
        }
        int c = 0;
        for (int k = 0; k < t_factors; k++)
            c = c + factors[k];
        int b = 2 * num;
        if (c < b)
            printf("%d is deficient\n", num);
        else
            printf("%d is not deficient\n", num);
    }
    return 0;
}

如果您输入30作为响应,则会产生:

Enter the total number of figures you want to check: 30
1 is deficient
2 is deficient
3 is deficient
4 is deficient
5 is deficient
6 is not deficient
7 is deficient
8 is deficient
9 is deficient
10 is deficient
11 is deficient
12 is not deficient
13 is deficient
14 is deficient
15 is deficient
16 is deficient
17 is deficient
18 is not deficient
19 is deficient
20 is not deficient
21 is deficient
22 is deficient
23 is deficient
24 is not deficient
25 is deficient
26 is deficient
27 is deficient
28 is not deficient
29 is deficient
30 is not deficient

您可以通过避免多种因素并在使用过程中累积术语来简化流程:

#include <stdio.h>

int main(void)
{
    int range;

    printf("Enter the total number of figures you want to check: ");
    if (scanf("%d", &range) != 1)
        return 1;

    for (int num = 1; num <= range; num++)
    {
        int s = 0;
        for (int i = 1; i <= num; i++)
        {
            if (num % i == 0)
                s += i;
        }
        if (s < 2 * num)
            printf("%d is deficient\n", num);
        else
            printf("%d is not deficient\n", num);
    }
    return 0;
}

它产生与第一个程序相同的输出,这令人鼓舞。如果您想打印出数据以证明“不足”与“不不足”的判断是正确的,则可以保留“因素”数组,但是,除了寻找因素之后,除了累加数组中的元素外,您无需做任何其他事情,您真的不需要它。

您可以查看Numbers — abundant, deficient, perfect and amicable,发现不需要在已检查的因子中包括num,并且不需要2 * num,并且可以“优化”通过不检查大于num / 2的数字(因为它们不是因素),得出:

#include <stdio.h>

int main(void)
{
    int range;

    printf("Enter the total number of figures you want to check: ");
    if (scanf("%d", &range) != 1)
        return 1;

    for (int num = 1; num <= range; num++)
    {
        int s = 0;
        for (int i = 1; i <= num / 2; i++)
        {
            if (num % i == 0)
                s += i;
        }
        if (s < num)
            printf("%d is deficient\n", num);
        else if (s > num)
            printf("%d is abundant\n", num);
        else
            printf("%d is perfect\n", num);
    }
    return 0;
}

输出是:

Enter the total number of figures you want to check: 30
1 is deficient
2 is deficient
3 is deficient
4 is deficient
5 is deficient
6 is perfect
7 is deficient
8 is deficient
9 is deficient
10 is deficient
11 is deficient
12 is abundant
13 is deficient
14 is deficient
15 is deficient
16 is deficient
17 is deficient
18 is abundant
19 is deficient
20 is abundant
21 is deficient
22 is deficient
23 is deficient
24 is abundant
25 is deficient
26 is deficient
27 is deficient
28 is perfect
29 is deficient
30 is abundant