C语言编写的冰雹序列
#include "stdio.h" #include "stdlib.h"</code> int getSubValue(const int &tmpvalue) { int newvalue = 0; if(tmpvalue <= 1) { newvalue = 1; } else if( tmpvalue % 2 == 0 ) { newvalue = tmpvalue/2; } else { newvalue = tmpvalue*3 + 1; } return newvalue; } void hailstone(const int &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",&num); hailstone(num); }