【发布时间】:2011-09-05 11:31:04
【问题描述】:
谁能帮我解决这个问题:这是一个查找任意长度字符串的所有排列的程序。需要相同的非递归形式。 (首选C语言实现)
using namespace std;
string swtch(string topermute, int x, int y)
{
string newstring = topermute;
newstring[x] = newstring[y];
newstring[y] = topermute[x]; //avoids temp variable
return newstring;
}
void permute(string topermute, int place)
{
if(place == topermute.length() - 1)
{
cout<<topermute<<endl;
}
for(int nextchar = place; nextchar < topermute.length(); nextchar++)
{
permute(swtch(topermute, place, nextchar),place+1);
}
}
int main(int argc, char* argv[])
{
if(argc!=2)
{
cout<<"Proper input is 'permute string'";
return 1;
}
permute(argv[1], 0);
return 0;
}
【问题讨论】:
-
这不是一个“C”语言解决方案——你使用的是内置的字符串类型、cout、运算符重载......如果它是由 Stroustrup 自己编写的,那就再好不过了 C++ .
-
你是对的。 [c] 标签被移除以保存一个编辑。
-
我在暗示我想要这个 C++ 代码的 C 实现
标签: c++ algorithm recursion iteration