我需要一个产生 1,2,3,4的组合:
   如:1
      1,2
      1,2,3
      1,2,3,4
      1,3
      1,3,4
      1,4
      2,3
      2,3,4
      2,4
      3,4
  如何用VC实现?另外,产生的组合需要用二维动态数组存储,如何定义与保存??  谢谢!!

解决方案 »

  1.   

    其实组合好实现,假设1234是否应该出现设定为0和1来表示,其实其组合的情况,就是从0x1到0xf的二进制表示。
    比如:16进制 0x1  二进制 0001 代表组合的第一种情况 1
         16进制 0x2  二进制 0010 代表组合的第一种情况 2
       .....  依次类推保存的方法 其实就是建立一个二位数组 INT  nResult[16][4] = {{1}, {2},{3},{4},{1,2},....{1,2,3,4}}依次类推所以VC代码实现为:#include "stdafx.h"int main(int argc, char* argv[])
    {
        int  nResult[16][4] = {0};
        
        int nEnum[] = {1, 2, 3, 4};
        
        for(int i = 0x1; i< 0xf; i++)
        {
            int nCounter = 0;
            for(int nColumn = 0; nColumn < 4; nColumn++)
            {
                if((i>>nColumn)&0x1 == 1)
                {
                    nResult[i - 1] [nCounter ++] = nEnum[nColumn];
                }
            }
        }
    return 0;
    }
      

  2.   

    修正一下#include "stdafx.h"int main(int argc, char* argv[])
    {
        int  nResult[16][4] = {0};
        
        int nEnum[] = {1, 2, 3, 4};
        
        for(int i = 0x1; i<= 0xf; i++)//范围为:0x0-0xf
        {
            int nCounter = 0;
            for(int nColumn = 0; nColumn < 4; nColumn++)
            {
                if((i>>nColumn)&0x1 == 1)
                {
                    nResult[i - 1] [nCounter ++] = nEnum[nColumn];
                }
            }
        }
        return 0;
    }
      

  3.   

    一共是15种情况,我申请的数组多了一组。到int  nResult[15][4] = {0};这个就够了
      

  4.   

    动态数组的版本const int ROW = 15;const int COL = 4;
    int main(){
        
        int iRow=0, iCol=0;     int nEnum[] = {1, 2, 3, 4};
        
        //´´½¨Êý×é
        
        cout<<"Create array"<<endl;
        
        int **ppint = new int*[ROW];
        
        for(iRow=0;iRow<ROW;iRow++){
            
            ppint[iRow] = new int[COL];
            
        }
        
        //½øÐмÆËã
        
        for(int i = 0x1; i<= 0xf; i++)//???:0x0-0xf
        {
            int nCounter = 0;
            
            for(int nColumn = 0; nColumn < 4; nColumn++)
            {
                if((i>>nColumn)&0x1 == 1)
                {
                    ppint[i - 1] [nCounter++] = nEnum[nColumn];
                }
                else
                {
                    ppint[i - 1] [nCounter++] = 0;
                }
            }
        }
        
        //Êä³ö
        
        cout<<"use the array"<<endl;
        
        for(iRow=0;iRow<ROW;iRow++){
            
            for(iCol=0;iCol<COL;iCol++){
                
                cout<<"["<<iRow<<"]["<<iCol<<"]"<<ppint[iRow][iCol]<<";"<<endl;
                
            }
            
            cout<<endl;
            
        }
        
        //ɾ³ý
        
        cout<<endl<<"Now, delete the array"<<endl;
        
        for(iRow=0;iRow<ROW;iRow++){
            
            delete[] ppint[iRow];
            
        }
        
        delete[] ppint;
        ppint = NULL;
        
        
        //
        
        cout<<"Press return to exit......."<<endl;
        
        getchar();
        
        return 0;
        
    }
      

  5.   


    如果我不能用常量ROW,COL,要每产生一个组合数,才插入。好像可以用vector方法,具体怎样,不会呀???
      

  6.   

    为什么显示出的值不是INT  nResult[16][4] = {{1}, {2},{3},{4},{1,2},....{1,2,3,4}}这种形式的??
      

  7.   

    你说的是二位动态数组,又没说vector,晚上给你写代码