冰雹序列-C语言

C语言编写的冰雹序列


#include "stdio.h"
#include "stdlib.h"</code>

int getSubValue(const int &amp;tmpvalue)
{
    int newvalue = 0;
    if(tmpvalue &lt;= 1)
    {
        newvalue = 1;
    }
    else if( tmpvalue % 2 == 0 )
    {
        newvalue = tmpvalue/2;
    }
    else
    {
        newvalue = tmpvalue*3 + 1;
    }
    return newvalue;
}

void hailstone(const int &amp;tmpvalue)
{
    int count = 0;
    int newvalue = tmpvalue;
    printf("hailstone( %d ) = { ",tmpvalue);
    while( newvalue != 1 )
    {
        if( 0 != count )
        {
             printf( " , ");
        }
        count++;
        newvalue = getSubValue(newvalue);
        printf("%d",newvalue);
    }
    printf(" } \n");
}

int main(void)
{
    int num ;
    int count = 0;
    printf("please input the first value: ");
    scanf("%d",&amp;num);
    hailstone(num);
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注