码迷,mamicode.com
首页 > 其他好文 > 详细

全排列

时间:2014-09-19 07:42:05      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   div   sp   

一种全排列是将n个数字放入n个位置里,一种是n个位置上任意位置都可以取0,1,2,...,m,位置之间没有约束

#include <iostream>
using namespace std;
void permutation(char *pStr, char *pBegin, int &count){
    if (*pBegin == \0){
        printf("%s\t", pStr);
        count++;
    }else{
        for (char *pCh = pBegin; *pCh != \0; ++pCh){
            
                char temp1 = *pCh;
                *pCh = *pBegin;
                *pBegin = temp1;
            
            permutation(pStr, pBegin + 1,count);
            
                temp1 = *pCh;
                *pCh = *pBegin;
                *pBegin = temp1;
            
        }
    }

}
int main(){
    char pS[] = "123456";
    int count = 0;
    permutation(pS, pS, count);
    printf("%d", count);
    system("pause");
}
#include <iostream>
using namespace std;
void permutation(char *pStr, char *pBegin, int &count){
        if (*pBegin == \0){
            printf("%s\t", pStr);
            count++;
        }else{
            for (int i = 0; i < 10;i++){
                
                *pBegin = i + 0;
                
                permutation(pStr, pBegin + 1,count);
                
                
            
            }
        }
    
}
int main(){
    const int n = 3;
    int count = 0;
    char *pStr = new char[n+1];
    pStr[n] = \0;
    pStr[0] = pStr[1] = pStr[2] = 0;
    permutation(pStr, pStr, count);
    printf("%d\n", count);
    delete[]pStr;
    system("pause");
}

 

全排列

标签:style   blog   color   io   os   ar   for   div   sp   

原文地址:http://www.cnblogs.com/hzmbbbb/p/3980587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!