
程序代码:
/*
将标号为1.2.3.4.5.6的6张卡片放入3个不同的信封,
若每个信封放2张,有多少种放法
*/
#define NUM_OF_ENVELOPE ( 3 )
#define MAX_NUM_IN_ENVELOPE ( 2 )
#define NUM_OF_CARD ( 6 )
#define BEGIN ( 0 )
#define END ( NUM_OF_CARD + 1 )
#define RECORD ( MAX_NUM_IN_ENVELOPE - 1 + 1 )
#define NOTHING ( 0 )
#include <stdio.h>
#include <stdlib.h>
void put_card_to_envelope(void);
void out_put(int (*)[ MAX_NUM_IN_ENVELOPE + 1 ] , int , int );
int main( void )
{
put_card_to_envelope();
system("PAUSE");
return 0;
}
void put_card_to_envelope(void)
{
static int num_of_card = BEGIN ;
static int envelope[ NUM_OF_ENVELOPE ][ MAX_NUM_IN_ENVELOPE + 1 ] = {NOTHING};
static int kinds_of_put_method = 0 ;
switch( num_of_card ){
int i , j ;
case BEGIN :
num_of_card ++ ;
put_card_to_envelope();
printf( "有%d种放法\n" , kinds_of_put_method ) ;
num_of_card = BEGIN ;
kinds_of_put_method = 0 ;
break;
default :
for( i = 0 ; i < NUM_OF_ENVELOPE ; i++ )
{
if( envelope[ i ][ RECORD ] < MAX_NUM_IN_ENVELOPE ){
envelope[ i ][ envelope[i][RECORD] ] = num_of_card ;
envelope[ i ][ RECORD ] ++ ;
num_of_card++;
put_card_to_envelope();
num_of_card--;
envelope[ i ][ RECORD ] -- ;
envelope[ i ][ envelope[ i ][ RECORD ] ] = NOTHING ;
}
}
break;
case END :
out_put( envelope , NUM_OF_ENVELOPE , MAX_NUM_IN_ENVELOPE );
kinds_of_put_method++;
break;
}
}
void out_put(int (*p_en)[ MAX_NUM_IN_ENVELOPE + 1 ] , int n_en , int max_num )
{
int i , j ;
for( i = 0 ; i< n_en ; i ++ ){
for( j = 0 ; j < max_num ; j++ ){
printf("%d " , p_en[i][j] );
}
putchar('|');
}
putchar('\n');
}
[
本帖最后由 键盘农夫 于 2011-6-7 14:59 编辑 ]