简体   繁体   English

我需要为学校做一个程序“给定一个数字确定其主要除数的总和”,但我不明白为什么我的程序不起作用

[英]I need to do a program for school “given a number establish the sum of its prime divisors” but I don't understand why my program doesn't work

I don't understand why it doesn't work.我不明白为什么它不起作用。 When I run the program it tells me that the sum is equal to 0.当我运行程序时,它告诉我总和等于 0。

int main()  {
    system("CLS"); system("COLOR 0a");
    int i,num,j,cont=0,somma=0;
    printf("enter a number");       //the user enter a number
    scanf("%d",&num);
    for(i=2;i<=num;i++)      //the for and the if detects all divisors of the number entered except 1
        if(num%i==0){ 
            for(j=2;j<i;i++) //this for and if detect the divisors of the divisor of the entered number
                if(i%j==0)
                    cont++; 
            {
                if(cont==0) //if cont == 0 the divisor of the number is prime and is added to the sum
                    somma=somma+i;
            }
            printf("the sum of the prime divisor of %d is %d",num,somma);
        }

}

Curly brackets after for and if are only required when you use more than one statement, but as a beginner you should learn to always use them unless you have a single line statement like if (i == 0) j=1; for 和 if 后面的花括号仅在您使用多个语句时才需要,但作为初学者,您应该学会始终使用它们,除非您有单行语句,例如if (i == 0) j=1; . . And even if it is not required care for indentation.即使不需要注意缩进。 It will make the structure of the program more evident at first sight.它将使程序的结构一目了然。 And it matters...这很重要...

Then you should learn to be very cautious with short variable names: for(j=2;j<i;i++) increments i when it should increment j .然后你应该学会对短变量名非常谨慎: for(j=2;j<i;i++)在应该增加i时增加j

Finaly you should always wonder at what time a variable should be initialized.最后,您应该总是想知道什么时候应该初始化变量。 You use cont inside a double loop, and initialize it to 0 only at the beginning of the program.您在双循环中使用cont ,并且仅在程序开始时将其初始化为 0。 It should be reset to 0 on every iteration of the outer loop.它应该在外循环的每次迭代中重置为 0。

And last but not least: learn to use a debugger , or use trace print statements throughout your code.最后但同样重要的是:学习使用调试器,或在整个代码中使用跟踪打印语句。 If you had, the problems would have been self evident - in fact I did it...如果你有,问题将是不言而喻的 - 事实上我做到了......

Fix all that and the program should give the expected results.修复所有这些,程序应该会给出预期的结果。 Or ask a new question if it is still broken.或者问一个新问题,如果它仍然坏掉。 But when it will work, I strongly advice you to post it in code review.但是当它起作用时,我强烈建议您将其发布在代码审查中。 You will get good advices there both on the C language part and on the algorithmic one.您将在 C 语言部分和算法部分获得很好的建议。


Here is a minimaly fixed version of your code with the (commented out) traces that helped to identify the problems:这是您的代码的最小固定版本,带有(注释掉)有助于识别问题的跟踪:

int main()  {
    int i,num,j,cont,somma=0;
    printf("enter a number");
    scanf("%d",&num);
    //printf("Processing %d\n", num);
    for(i=2;i<=num;i++) {
        //printf("i:%d", i);
        if(num%i==0){
            cont = 0;          // must be reset to 0 for every i
            for(j=2;j<i;j++) {
                //printf("j:%d\n", j);
                if(i%j==0) cont++;
            }
            if(cont==0) somma=somma+i;
        }
    }
    printf("the sum of the prime divisor of %d is %d",num,somma);
    return 0;
}

Please do not blindly use it but try to understand the differences and do not forget the Code Review step...请不要盲目使用它,但要尝试理解差异,不要忘记代码审查步骤......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM