萌新80分求助!#5RE 求大佬指点
查看原帖
萌新80分求助!#5RE 求大佬指点
1055665
Twantifred楼主2024/3/11 20:45

代码如下

#include <stdio.h>    
#include <stdint.h>         
#include <inttypes.h>       

/*  给出一个正整数 n,然后对这个数字一直进行下面的操作:
    如果这个数字是奇数,那么将其乘 3 再加 1,否则除以 2。经过若干次循环后,
    最终都会回到 1。经过验证很大的数字(7×10^11)都可以按照这样的方式比变成 1,
    所以被称为“冰雹猜想”。例如当 n 是 20,变化的过程是 20→10→5→16→8→4→2→1。*/


int main(void)
{			    
    uint32_t number;      //正整数 n
    scanf("%" SCNu32, &number);       //正常输入一个正整数 n

    uint32_t number_of_counting_process[100];         //过程数

    uint32_t time = 0;      //计算次数
    number_of_counting_process[0] = number;         //逆推第一个数为它本身

    //进行逆推
    while( number != 1 )
    {
        if(number%2==0)
        {
            number/=2;
            number_of_counting_process[++time] = number;
        }
        else
        {
            number= number*3 + 1;
            number_of_counting_process[++time] = number;
        }
    }
    
    //输出
    for( int32_t index = (int32_t)time ; index >= 0 ; index --)
    {   
        //特判
        if(index == 0)
        {
            printf("%" PRIu32, number_of_counting_process[index]);
            break;
        }

        //正常输出
        printf("%" PRIu32 " ", number_of_counting_process[index]);
    }

	return 0;
}				        


2024/3/11 20:45
加载中...