Chinaunix首页 | 论坛 | 博客
  • 博客访问: 229338
  • 博文数量: 39
  • 博客积分: 1130
  • 博客等级: 少尉
  • 技术积分: 453
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-26 15:54
文章分类

全部博文(39)

文章存档

2012年(1)

2011年(31)

2010年(7)

分类: C/C++

2011-06-21 20:27:32

  1. /*
  2. 将标号为1.2.3.4.5.6的6张卡片放入3个不同的信封,
  3. 若每个信封放2张,有多少种放法
  4. */
  5. #define NUM_OF_ENVELOPE ( 3 )
  6. #define MAX_NUM_IN_ENVELOPE ( 2 )
  7. #define NUM_OF_CARD ( 6 )
  8. #define BEGIN ( 0 )
  9. #define END ( NUM_OF_CARD + 1 )
  10. #define RECORD ( MAX_NUM_IN_ENVELOPE - 1 + 1 )
  11. #define NOTHING ( 0 )
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. void put_card_to_envelope(void);
  15. void out_put(int (*)[ MAX_NUM_IN_ENVELOPE + 1 ] , int , int );
  16. int main( void )
  17. {
  18.   put_card_to_envelope();
  19.   system("PAUSE");
  20.   return 0;
  21. }
  22. void put_card_to_envelope(void)
  23. {
  24.     static int num_of_card = BEGIN ;
  25.     static int envelope[ NUM_OF_ENVELOPE ][ MAX_NUM_IN_ENVELOPE + 1 ] = {NOTHING};
  26.     static int kinds_of_put_method = 0 ;
  27.     switch( num_of_card ){
  28.          int i , j ;
  29.          case BEGIN :
  30.             num_of_card ++ ;
  31.             put_card_to_envelope();
  32.             printf( "有%d种放法\n" , kinds_of_put_method ) ;
  33.             num_of_card = BEGIN ;
  34.             kinds_of_put_method = 0 ;
  35.             break;
  36.          default :
  37.             for( i = 0 ; i < NUM_OF_ENVELOPE ; i++ )
  38.             {
  39.               if( envelope[ i ][ RECORD ] < MAX_NUM_IN_ENVELOPE ){
  40.                   envelope[ i ][ envelope[i][RECORD] ] = num_of_card ;
  41.                   envelope[ i ][ RECORD ] ++ ;
  42.                   num_of_card++;
  43.                   put_card_to_envelope();
  44.                   num_of_card--;
  45.                   envelope[ i ][ RECORD ] -- ;
  46.                   envelope[ i ][ envelope[ i ][ RECORD ] ] = NOTHING ;
  47.               }
  48.             }
  49.             break;
  50.          case END :
  51.             out_put( envelope , NUM_OF_ENVELOPE , MAX_NUM_IN_ENVELOPE );
  52.             kinds_of_put_method++;
  53.             break;
  54.          }
  55. }
  56. void out_put(int (*p_en)[ MAX_NUM_IN_ENVELOPE + 1 ] , int n_en , int max_num )
  57. {
  58.     int i , j ;
  59.     for( i = 0 ; i< n_en ; i ++ ){
  60.       for( j = 0 ; j < max_num ; j++ ){
  61.          printf("%d " , p_en[i][j] );
  62.        }
  63.       putchar('|');
  64.     }
  65.     putchar('\n');
  66. }

阅读(1763) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~