C语言: 高亮代码由发芽网提供
void print_partition ( int n )
{ int i = 1;
int m = 1;
int h = 1;
int t , r ;
int a[ n+1 ] ;
for ( ; i < n + 1 ; ++ i ) a[ i ] = 1 ;
a[ 1 ] = n ;
printf ( "%d \n", a[ 1 ] ) ;
while ( a[ 1 ] ! = 1 )
{ if ( a [ h ] == 2 ) { a[ h-- ] -- ; m++ ; }
else { r = --a[ h ];
t = m - h + 1 ;
while ( t >= r ) { a[ ++h ] = r ; t -= r ; }
if ( t == 0 ) m = h ;
else m = h + 1 ;
if ( t >= 2 ) a[ ++h ] = t ;
}
for ( i = 1 ; i < m + 1 ; i++ )
printf ( "%d ", a[ i ] ) ;
printf ( "\n" ) ;
}
}
整数分拆
时间: 2024-12-26 18:23:04