代码--C++全排列和子集的实现

 "act.h"
/*
* File:   act.h
* Author: Napoleon
*
*/
#ifndef _ACT_H
#define _ACT_H
class act{
    static int number;
public:
    act();
    act(const act& orig);
    virtual ~act();
    void Perm(int list[], int k, int m);
    void setList(int *list,int length);
    void SubSet(int *List, int m, int *Buffer, int flag);
    void Output(int *Buffer, int flag);
    int Index(int *List, int c);
private:
};
#endif /* _ACT_H */


"act.cpp"
/*
* File:   act.cpp
* Author: Napoleon
*
*/
#include "act.h"
#include <stdio.h>
#include <iostream.h>
using namespace std;
inline void Swap(int& a, int& b);
int ListLength;
void SubSet(int *List, int m, int *Buffer, int flag);
void Output(int *Buffer, int flag);
int Index(int *List, int c);
act::act() {
}
act::act(const act& orig) {
}
act::~act() {
}
void setList(int *list,int length) {
    ListLength = length;
    int *Buffer=new int[ListLength];
    for(int i=0;i<ListLength;i++)
  Buffer[i]=' ';
    SubSet(list,0,Buffer,0);
}
void Perm(int list[], int k, int m) {
    int i;
    if (k == m) {
        for (i = 0; i <= m; i++) {
            cout << list[i];
        }
        cout << endl;
    } else {
        for (i = k; i <= m; i++) {
            Swap(list[k], list[i]);
            Perm(list, k + 1, m);
            Swap(list[k], list[i]);
        }
    }
}
inline void Swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}
int Index(int *List, int c)
{
     for(int i=0; i<=ListLength-1; i++)
     {
              if(c==List[i])
              {
                    return i;
                    break;
              }
     }
     return -1;
}
void SubSet(int *List, int m, int *Buffer, int flag)
{     void Output(int *Buffer, int flag);
     if(m <= ListLength-1)
     {

          for(int i=(flag==0) ? 0 : Index(List,Buffer[flag-1])+1; i<=ListLength-1; i++)
        
          {
                Buffer[flag]=List[i];
                Output(Buffer,flag);
                SubSet(List, m+1, Buffer,flag+1);
          }
     }
     //return;
}

void Output(int *Buffer, int flag)
{
     static int count=1;
     if(count==1)
     {
              cout<<count++<<": { }"<<endl;
     }
     cout<<count++<<": {";
     for(int i=0; i<=flag; i++)
     {
              cout<<Buffer[i];
     }
     cout<<"}"<<endl;
}



"Main.cpp"
/*
* File:  Main.cpp
* Author: Napoleon
*
*/
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>
#include "act.h"
void Perm(int list[], int k, int m);
void setList(int *list,int length);
int main() {
    cout << "Please input the number of all your numbers!" << "/n";
    int num;
    cin >> num;
    cout << "Now,input all the numbers(one in a line.)" << "/n";
    int queue[num];
    for (int i = 0; i < num; i++) {
        cin >> queue[i];
    }
    cout << "Now I will output all the sequences of them." << "/n";
    Perm(queue, 0, num - 1);
    cout << "/n";
    
    cout << "Then all of its subsets are shown below:/n";
    setList(queue,num);
    return (EXIT_SUCCESS);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值