第二周训练 | 搜索技术 4.1递归和排列

打印从n个数的全排列:

#include<iostream>
#include<algorithm>
#include<ctime>
#define Swap(a,b) {int temp=a;a=b;b=temp;}
using namespace std;
int data[]={1,2,3,4,5,6,7,8,9,10,32,15,18,33};
int num = 0;
int Perm(int begin,int end)
{
	int i;
	if(begin==end)
	{
		num++;
	}
	else
	{
		for(i=begin;i<=end;++i)
		{
			Swap(data[begin],data[i]);
			Perm(begin+1,end);
			Swap(data[begin],data[i]);
		}
	}
}
int main()
{
	clock_t start,end;
	start = clock();
	Perm(0,9);
	end = clock();
	cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
	cout<<num<<endl;
	return 0;
}
 

解析:以perm(0,2)为例:

打印从n个数种选取m个数的组合数:

#include<iostream>
#include<algorithm>
#include<ctime>
#define Swap(a,b) {int temp=a;a=b;b=temp;}
using namespace std;
int data[]={1,2,3,4,5,6,7,8,9,10,32,15,18,33};
int num = 0;
int Perm(int begin,int end)
{
    int i;
    if(begin==2)
    {
        num++;
        cout<<num<<":"<<data[0]<<data[1]<<endl;
    }
    else
    {
        for(i=begin;i<=end;++i)
        {
            Swap(data[begin],data[i]);
            cout<<"Swap:"<<begin<<","<<i<<endl;
            cout<<"Perm:"<<begin+1<<","<<end<<endl;
            Perm(begin+1,end);
            Swap(data[begin],data[i]);
        }
    }
}
int main()
{
    clock_t start,end;
    start = clock();
    Perm(0,3);
    end = clock();
    cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
    cout<<num<<endl;
    return 0;
}
 

解析:以4取2为例,这里有2个位置第一个位置有4种取法,第二个有3种取法,4X3=12

 4.2子集生成和组合

二进制表示子集:

#include<iostream>
using namespace std;
void print_subset(int n)
{
    for(int i=0;i<(1<<n);++i)
    {
        for(int j=0;j<n;++j)
            if(i&(1<<j))
                cout<<j<<" ";
        cout<<":"<<i+1<<"<<"<<endl;
    }
}
int main()
{
    int n;
    cin>>n;
    print_subset(n);    
} 

 打印n个数中任意m个数的组合:

将这个问题转换为列出一个数集(大小为n)的子集,统计其中元素个数为m的集合有几个

#include<iostream>
using namespace std;
void print_set(int n,int k)
{
    for(int i=0;i<(1<<n);++i)
    {
        int num = 0,kk = i;
        while(kk)
        {
            kk = kk&(kk-1);
            num++;
        }
        if(num==k)
        {
            for(int j=0;j<n;++j)
            {
                if(i&(1<<j))
                {
                    cout<<j<<",";
                }
            }
            cout<<endl;
        }
        
    }
}
int main()
{
    int n,k;
    cin>>n>>k;
    print_set(n,k);
}

原文地址:https://www.cnblogs.com/chrysanthemum/p/11835181.html