最简单的:
for()
{
  for()
  {
    for()
    {
      for()
      {
        for() {}
      }
    }
  }
}

解决方案 »

  1.   

    对于盒子可以保存一个bool数组表示,序号表示盒子,bool变量表示盒子有没有被占用,在最外面的循环把数组初始化!不就OK了!我建议你看看《离散数学》和《数据结构》!
      

  2.   

    我这里有一个源程序,可以找出1-9数字的所有组合从123456789到987654321一共9!种情况,按你的意思,你的问题为1,2,3,4,5的所有组合。
    /**********************************************************
    *   This source program is to find the all combinations   *
    *     of several numbers,for example,numbers 0 to 9.      *
    * Author:An Xiangbin,2001-8-19,Email:[email protected]   *
    **********************************************************/
    #include <conio.h>
    #include <stdio.h>
    #define N 9
    int a[N+1];
    long count=0;void init(int *a,int n);
    void print(int *a,int n);
    void work(int *a,int n);
    void swap(int *p,int *q);
    void sort(int *a,int alen);void init(int *a,int n){
    int i;
      for(i=0;i<n+1;i++) a[i]=i;
    }
    /**************************************
     *  Sort a array,length(array)=alen   *
     **************************************/
    void sort(int *a,int alen){
    int *p,*q,*k;
      for(p = a;p < (a+alen-1);p++){
        k = p;
        for(q=p+1;q<a+alen;q++)
          if(*p > *q) k = q;
        if(k!=p)swap(p,k);
      }
    }
    /**************************************************
     *   get the all combinations of these numbers    *
     **************************************************/
    void work(int *a,int n){
      int p,i,j,k,t,flag=0;  p=n;
      while(p>0){
        flag = 0;
        j = p;
        for(i=n;i>p;i--)
          if(a[i] > a[j]) {
    flag = 1;
    break;
          }
        if(flag){
          swap(&a[j],&a[i]);
          sort(a+j+1,n-j);
          count++;
          print(a,n);
          for(i=j;i<=n;i++)
    if(a[i]<a[i+1])p++;
        }
        else p--;
      }
    }void swap(int *p,int *q){
      int t;
      t  = *p;
      *p = *q;
      *q = t;
    }void print(int *a,int n){
      int i;
      for(i = 1;i < n+1;i++)
        printf("  %d",a[i]);
      printf("\n");
      getch();
    }void main(void){  clrscr();
      printf("\nPress any key to begin...\n");
      getch();
      init(a,N);
      count=1;
      print(a,N);
      work(a,N);
      printf("total solution is  %ld: ",count);
      getch();
    }
    /********************************************
    numbers are 1,2,3
    Press any key to begin...
      1  2  3
      1  3  2
      2  1  3
      2  3  1
      3  1  2
      3  2  1
    total solution is  6:
      

  3.   

    (你不说我倒还明白,你越说我越糊涂)说的也没错,不过忽略了一个问题,就是1,2,3,4,5五个数的全排列,不是找密码。其实用循环也可以实现,不过应该这么写
      int i,j,k,m,n ,t=0;
      for (i=1;i< 6 ; i++)
        for (j=1 ; j<6 ; j ++ )
         if( j <> i ){
           for ( k = 1 ;k<6;k++)
             if ( k <> i && k <> j ){
              for ( m = 1; m <6 ; m ++ )
                if ( m <> k && m <> j && m<> i ){
                  for ( n =1 ; n < 6 ; n ++ ) 
                    if( n<>m && n <>k && n<>j && n <> i) {
                      printf("solution[%d]:%d ,%d ,%d ,%d ,%d ,%d\n",
                             t++,i,j,k,m,n);
                     }
                 }
              }
          }
      呵呵,如果是1-20呢?好象写出来没人能看明白了吧!