zoukankan      html  css  js  c++  java
  • 37-字符的全排列

    题目内容:

    对字符串(数字,字母,符号)进行全排列,并统计全排列的种树

    输入描述

    输入一个字符串


    输出描述

    输出字符串的全排列,每种情况占一行,最后一行输出全排列的个数


    输入样例

    123


    输出样例

    123
    132
    213
    231
    312
    321
    6

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    string str;
    int coun;
    string s[100];
    
    int mySwap(int i, int j){
    	char temp;
    	temp = str[i];
    	str[i] = str[j];
    	str[j] = temp;
    }
    
    int isswap(int i, int j){
    	for(int k = i; k < j; k++){
    		if(str[k] == str[j])
    			return 0;
    	}
    	return 1;
    } 
    
    int allpai(int i, int n){
    	int j;
    	if(i == n - 1){
    //		cout << str << endl;
    		s[coun++] = str;
    	}
    	else{
    		for(int j = i; j < n; j++){
    			if(isswap(i, j)){
    				mySwap(i, j);     //for循环每一次交换第一个与后面的每一个的位置 
    	//			cout << "test1 : i j " << i << " " << j << " str " << str << endl;
    				allpai(i + 1, n); //从i = 0开始即确定了第一个位置,重复上面for循环动作,依次确定第二个位置 
    				mySwap(j, i);     //对到初始状态,为下个for循环做准备 
    	//			cout << "test1 : i j " << i << " " << j << " str " << str << endl;
    			}
    		}
    	}
    }
    
    bool cmp(string a, string b){
    	return a < b;
    }
    
    int main(){
    
    	cin >> str;
    	allpai(0, str.length());
    //	cout << "sort" << endl;
    	sort(s, s + coun, cmp);	
    	for(int i = 0; i < coun; i++)
    		cout << s[i] << endl;
    	cout << coun;
    	return 0;
    }
    
  • 相关阅读:
    注册界面测试案例注意点
    linux常用命令
    linux常用快捷键总结
    2015.8.29某高级企业的在线笔试题
    图像处理------直方图均衡化
    “猫叫系统”开启了观察者模式
    Hua Wei 机试题目四---2014
    Hua Wei 机试题目三---2014
    Hua Wei 机试题目二
    Hua Wei 机试题目一
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/7735058.html
Copyright © 2011-2022 走看看