gpt4 book ai didi

c++ - Knuth 置换算法奇怪的行为

转载 作者:行者123 更新时间:2023-11-27 23:33:26 24 4
gpt4 key购买 nike

我附上了一段代码,它根据 cout 语句给出了奇怪的输出。该程序主要计算 Knuth 的排列。

输入是说:run1代码运行良好,第一次通过:调用跟踪将是:
r un1
你的 n1
努尔 1
1nur
n1ur
nu1r
nur1
在这段代码运行之后,调用正确返回到步骤 where
urn 1
在那里,但它不处理“RETURN”语句下面的代码。

此外,如果在完成排列的循环中假设有一个 cout,它甚至不会在 return 语句下面打印 cout

请让我知道我的代码是否存在任何基本缺陷或逻辑错误?

    #include <iostream>
using namespace std;

void swap( char *l, char *m )
{
char t = *l;
*l = *m;
*m = t;
}
void Permute( char *result, char *temp, int len )
{
int k = 0;
int j = 0;
char d[ 1000000];
int i = 0;
//cout << " Start of Perm " << result << " Stack: " << temp << endl;
while( result[ i ] != '\0' )
{
if( temp[ k ] !='\0' )
{
cout << " Start of Perm " << result << " Stack: " << temp << endl;
strncpy( d, &temp[ k ], sizeof( char ) );
strncat( d, result, sizeof( result ) );
strncat( d, "\0", sizeof( char ) );
cout << " Principal: " << d << endl;
k = k + 1;
if( temp[ k ] != '\0' )
Permute( d, &temp[ k ], len );
else
{
char d1[ 10000 ];
strncpy( d1, &temp[ k ], sizeof( char ) );
strncat( d1, d, sizeof( d ) );
strncat( d, "\0", sizeof( char ) );
strncpy( d, d1, sizeof( d ) );
//cout << "Final Level: " << d << endl;
strncpy( result, d, sizeof( d ) );
}
}
//cout << strlen( result ) << " == length which is " << len << " and result is: " << result << endl;
if( strlen( result ) >= len )
{
//cout << " Permutation Sets" << endl;
char result1[ 1000 ];
memcpy( result1, result, sizeof( result ) );
for( int p = 0; result1[ p ] != '\0'; p++ )
{
cout << "End : " << result1 << endl;
if( result1[ p + 1 ] != '\0' )
swap( &result1[ p ], &result1[ p + 1 ] );
}
return;
}
cout << " Value of I is: " << i << " and value of K is: " << k << endl;
if( result[ i + 1 ] != '\0' )
{
swap( &result[ i ], &result[ i + 1 ] );
k = 0;
d[ 0 ] = '\0';
cout << "New Branch: Value = " << result << " and stack = " << temp << endl;
}
i = i + 1;
}
}



int main( int argc, char *argv[] )
{
char c[100], temp[100];
cin >> c;
// cout << c << endl;
memcpy( temp, c, sizeof(c) );
// cout << temp << endl;
char c1[2];
c1[0] = c[0];
c1[1] = '\0';
Permute( c1, &temp[1], strlen( c ) );
}

谢谢!

最佳答案

如果这不是为了个人教育,你真的应该使用预定义函数 next_permutation .它非常易于使用:

#include <algorithm>
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::sort;

int main() {
string s("run1");

sort(s.begin(), s.end());

do {
cout << s << "\n";
} while (next_permutation(s.begin(), s.end()));

return 0;
}

如果你真的需要用 run1 开始排列,您仍然可以生成 vector<int> 的排列包含 {0, 1, 2, 3}然后通过以下代码构建中间字符串:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::string;
using std::vector;

int main() {
string s("run1");

vector<int> indexes;
for (size_t i = 0; i < s.size(); i++)
indexes.push_back(i);

do {
string tmp("");
for (size_t i = 0; i < indexes.size(); i++)
tmp += s[indexes[i]];
cout << tmp << "\n";
} while (next_permutation(indexes.begin(), indexes.end()));

return 0;
}

关于c++ - Knuth 置换算法奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3099708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com