13_栈的递归实例4---全排列

来源:互联网 发布:为什么ofo网络异常 编辑:程序博客网 时间:2024/06/07 00:27
#include "linkStack.h"static int number = 0;void hanoi( char *s , int a , int b ) {    if ((0 <= a) && (a <= b)){        if (a == b) {            cout << s << endl;            ++number;        }        else {            for (int i = a; i <= b; ++i) {                if ( (i>a)&&(s[i] == s[a]) )                         continue;                char temp = s[a];                s[a] = s[i];                s[i] = temp;                hanoi(s, a + 1, b);                temp = s[a];                s[a] = s[i];                s[i] = temp;            }        }    }}int main(int argc, char **argv) {    char s[] = "abca";    hanoi( s , 0 , 3);    cout << number << endl;    system( "pause" );    return 0;}
原创粉丝点击