itsource

재귀적 콜라츠 추측 함수 프로그램이 출력을 제공하지 않습니다.

mycopycode 2022. 10. 26. 22:38
반응형

재귀적 콜라츠 추측 함수 프로그램이 출력을 제공하지 않습니다.

그래서 저는 첫 번째 재귀 함수를 쓰려고 합니다.이것은 사용자에게 번호를 묻고, 그 입력을 받아 콜라츠 추측을 사용하여 1로 만듭니다.그런 다음 1과 기능을 실행해야 하는 횟수를 인쇄합니다.

코드가 컴파일되어 실행 시 사용자에게 번호를 요구하지만 값이 반환되지 않습니다.

도와주시면 감사하겠습니다.저는 초보자이니 두 번 이상의 어리석은 실수를 예상하세요.

코드는 다음과 같습니다.

#include <stdio.h>
#include <cs50.h>

int j=0;
int collatz(int n);

int main(void)
{
int n = get_int("enter your number:\n");
    if (n>0)
        return collatz(n);
    else
    {
        return 1;
        printf("error");
    }
}

int collatz(int n)
{
    if (n==1)
    {
        return 1;
        printf("1");
        printf("You recursed: %d", j);
    }
    else if ((n%2)==0)
    {
            n = (n/2);
            (j++);
            return collatz(n);
    }
    else
        {
            return collatz((3*n)+1);
            (j++);
        }
}

잘 부탁드립니다!

언급URL : https://stackoverflow.com/questions/73599283/recursive-collatz-conjecture-function-programme-doesnt-give-any-output

반응형