为什么循环增量而不打印出循环次数?

时间:2022-06-29 09:05:41

Please I have my code below. I am trying to get 8, 3, 1, 8, 1, 1, 1, 1, 1, 1. For the cycles in the output of my code, when the user inputs 45 23 6 12 0 0 0 0 0 0. But for some reason it adds up the next number subtracted by 1. So I get 8, 10, 10, 17, 17, 17, 17, 17, 17, 17. I have tried to fix this but nothing I do seems to help. Can someone please help me point out the reason why this is happening?

我的代码如下。我试图获得8,3,1,8,1,1,1,1,1,1。对于我的代码输出中的周期,当用户输入45 23 6 12 0 0 0 0 0 0时。但由于某种原因,它将下一个数字减去1.所以我得到了8,10,10,17,17,17,17,17,17,17。我试图解决这个问题但我没做什么似乎有帮助。有人可以帮我指出这种情况发生的原因吗?

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

int sum_divisors(int); //Function Prototype

/*Just wanted to do an example to show you don't need
 *function prototypes if you write the function itself
 *before main.
 */
int cycles(int x){
    static int count = 1;
    int j;
    if(x == sum_divisors(x)){
        printf("%d%13d Cycle(s) \n", x, count);
    }else{
        x = sum_divisors(x);
        count++;
        printf("%d, ",x);
        cycles(x);
    }
}

int main(void){ //Start of main
    int count = 0;
    int x, sum;
    int i = 0;
    int nums [9];

    puts("Please enter 10 integers. Fill up the rest with 0s. Please only type in numbers if not you might not get the output you want.");

    while(scanf("%d ", &x) != EOF){
        if(x > 100){
            puts("Wrong input");
            exit(1);
        }else{
            count++;
            nums[i] = x;
            ++i;
        }           

        if(i == 9){
            break;
        }
    }

    for(i=0; i<count; i++){
        sum = sum_divisors(nums[i]);
        printf("%d, ", nums[i]);
        cycles(nums[i]);
    }

    puts("");

    return 0;
} //end of main

int sum_divisors(int n){

    int i;
    int sum = 0;
    int prime_ind = 0;

    if(n <= 1){
        sum = 0;
        return sum;
    }

    for(i=2; i<n; i++){
        if(n%i == 0){
            prime_ind = 1;
            break;
        }
    }

    if(prime_ind == 0){
        sum = 1;
    }else{
        for(i=1; i<((n/2)+1); i++){
            if(n%i == 0){
                sum += i;
            }
        }
    }
    return sum;
}

2 个解决方案

#1


2  

I have reviewed your program. Your problem is in the function

我查看了你的课程。你的问题在于功能

 int cycles(int x){}

so change it to

所以改成它

int cycles(int x){
    static int count = 1;
    int j;
    if(x == sum_divisors(x)){
        printf("%d%13d Cycle(s) \n", x, count);
        count=1; // add this line 

    }else{
        x = sum_divisors(x);
        count++;
        printf("%d, ",x);
        cycles(x);
    }
}

here static variable is not initialized when new cycle() called.

这里调用new cycle()时不会初始化静态变量。

#2


1  

int cycles(int x){
    static int count = 1;

A static variable like that will be initialized to the given value on the first execution of that line only. After that, it will keep its value across multiple function calls, not only the recursive ones but also "fresh" calls from main(). You just keep incrementing count. It will never be reset to 1.

像这样的静态变量将仅在该行的第一次执行时初始化为给定值。之后,它将在多个函数调用中保持其值,不仅是递归调用,还包括main()的“新”调用。你只是继续递增计数。永远不会重置为1。

I think that is not (exactly) what you wanted. It's hard to say, really, because you did not tell us what your functions are actually intended to do -- neither in the question nor, as I would have preferred, in comments. I'm not in a mind of reverse-engineering that information.

我认为这不是(完全)你想要的。很难说,真的,因为你没有告诉我们你的功能实际上是打算做什么 - 无论是在问题中,还是在我的评论中,我都不喜欢。我不介意对这些信息进行逆向工程。

I also stopped checking your code at that point, as the static is very likely to be the core of your problem. There might be more issues, like the out-of-bounds accesses hinted at by comments.

此时我也停止检查您的代码,因为静态很可能是您问题的核心。可能会有更多问题,例如评论暗示的越界访问。

#1


2  

I have reviewed your program. Your problem is in the function

我查看了你的课程。你的问题在于功能

 int cycles(int x){}

so change it to

所以改成它

int cycles(int x){
    static int count = 1;
    int j;
    if(x == sum_divisors(x)){
        printf("%d%13d Cycle(s) \n", x, count);
        count=1; // add this line 

    }else{
        x = sum_divisors(x);
        count++;
        printf("%d, ",x);
        cycles(x);
    }
}

here static variable is not initialized when new cycle() called.

这里调用new cycle()时不会初始化静态变量。

#2


1  

int cycles(int x){
    static int count = 1;

A static variable like that will be initialized to the given value on the first execution of that line only. After that, it will keep its value across multiple function calls, not only the recursive ones but also "fresh" calls from main(). You just keep incrementing count. It will never be reset to 1.

像这样的静态变量将仅在该行的第一次执行时初始化为给定值。之后,它将在多个函数调用中保持其值,不仅是递归调用,还包括main()的“新”调用。你只是继续递增计数。永远不会重置为1。

I think that is not (exactly) what you wanted. It's hard to say, really, because you did not tell us what your functions are actually intended to do -- neither in the question nor, as I would have preferred, in comments. I'm not in a mind of reverse-engineering that information.

我认为这不是(完全)你想要的。很难说,真的,因为你没有告诉我们你的功能实际上是打算做什么 - 无论是在问题中,还是在我的评论中,我都不喜欢。我不介意对这些信息进行逆向工程。

I also stopped checking your code at that point, as the static is very likely to be the core of your problem. There might be more issues, like the out-of-bounds accesses hinted at by comments.

此时我也停止检查您的代码,因为静态很可能是您问题的核心。可能会有更多问题,例如评论暗示的越界访问。