zoukankan      html  css  js  c++  java
  • 求一串字符串的全排列和所有组合


    //排列函数
    void arrangementofstr(char *str,int beg)//str指向待排列的数组,beg为数组的索引位置
    {
    	if (!str)
    	{
    		return ;
    	}
    	if (str[beg]=='')
    	{
    		cout<<str<<" ";
    		return;
    	}
    	for (int i=beg;str[i]!='';++i)
    	{ 
    			char tmp=str[beg];
    			str[beg]=str[i];
    			str[i]=tmp;
    		arrangementofstr(str,beg+1);
    		    tmp=str[beg];
    			str[beg]=str[i];
    			str[i]=tmp;
    	}
    }
    //组合函数
    void combination(char *pbeg,int n,int j)//pbeg为指向待组合的数组,n为组合数,j为数组索引位置
    {
    	if (!pbeg)
    		return;
    	static string tmp;
    	if (n==0)
    	{
    		cout<<tmp<<" ";
    		return;
    	}
    	for (int i=j;i<=strlen(pbeg)-n;++i)//i=j-------------------------!!!!!!!!注意啊啊 啊啊啊  
    	{
    		tmp.push_back(pbeg[i]);//strlen(pbeg)-n其实不变啦,为窗口大小,窗口向后移动
    		combination(pbeg+1,n-1,i);
    		tmp.pop_back();
    	}
    }


    
    

    void allcombination(char *p)
    {
    	for (int i=1;i<=strlen(p);++i)
    	{
    		combination(p,i,0);
    		cout<<endl;
    	}
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char a[]="abcde";
    	arrangementofstr(a,0);
    	//allcombination(a);
    	combination1(a,3);
    	return 0;
    }
    


    运行结果:


  • 相关阅读:
    锁屏设计文档
    用 jquery 解决 浏览器 兼容问题
    mysql 查询语句
    技术相关
    android Rom 制作2
    android Rom 制作
    UI设计
    jquery 表单验证
    cent os数据库安装
    mysql jdbc 驱动 下载地址官网
  • 原文地址:https://www.cnblogs.com/chhuach2005/p/3961695.html
Copyright © 2011-2022 走看看