#include <iostream>
using namespace std;
void func(char *str,char *pbegin)
{
if (*pbegin == '\0')
cout << str<<endl;
for (char *p = pbegin; *p != '\0'; p++)
{
swap(*pbegin, *p);
func(str, pbegin + 1);
swap(*pbegin, *p);
}
}
int main()
{
char str[4] = "abc";
func(str,str);
system("pause");
return 0;
}
或者用下标:
#include <iostream>
#include <string>
#include <stdio.h>
#include <cmath>
#include <vector>
using namespace std;
void show(char* begin,char *str)
{
if (*str == '\0')
cout << begin << endl;
for (int i = 0; i < strlen(str); i++)
{
cout <<"***********i="<<i << endl;
swap(str[0], str[i]);
show(begin, str + 1);
swap(str[0], str[i]);
}
}
int main()
{
char str[4] = "abc";
show(str, str);
system("pause");
}
或者用一个参数:
#include <iostream>
using namespace std;
int length = 0;
void func(char *str)
{
if (*str == '\0')
cout << (str - length) << endl;
for(char *pbegin = str; *pbegin != '\0'; pbegin++)
{
swap(*str, *pbegin);
func(str+1);
swap(*str,*pbegin);
}
}
int main()
{
char str[4] = "abc";
length = strlen(str);
func(str);
system("pause");
return 0;
}