数据结构与算法分析 编程题

编写带有下列声明的例程(用C++):
void permute( const string&str);
void permute( const string&str,int low,int high);
用递归

void permute(const std::string str, int low = 0, int high = -1)
{
std::string cpy = str;
int len = ((high == -1)?cpy.length():high);
int a, b, t;
a = b = t = len-1;
if(len == 1)
std::cout << cpy << std::endl;
while(true)
{
a = b = len-1;
while(--a >= low && cpy[a] > cpy[a+1]);
if(a < low) break;
t = a+1;
while(b > a && cpy[b--] < cpy[a]);b++;
cpy[a] ^= cpy[b] ^= cpy[a] ^= cpy[b];
std::reverse(&cpy[t], &cpy[len]);
std::cout << cpy << std::endl;
}
}

温馨提示:答案为网友推荐,仅供参考