0

coutステートメントに基づいて奇妙な出力を与えるコードを添付しました。このプログラムは基本的にKnuthの順列を計算します。

入力は次のとおりです。run1コードは最初のパスで正常に実行されます:呼び出しトレースは次のようになります:
r un1
ur n1
nur 1
1nur
n1ur
nu1r
nur1このコードの実行後、呼び出しは 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 ) );
}

ありがとう!

4

3 に答える 3

2

これが個人的な教育用でない場合は、事前定義された関数を実際に使用する必要があります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;
}
于 2010-07-28T07:25:30.727 に答える
1

gdb などのデバッガーを使用して、値を確認しながらプログラムを 1 行ずつ実行します。

于 2010-06-23T07:56:35.337 に答える
1

C++ の場合、ヘッダーstringで classを使用する必要があります。<string>これにより、コードがより安全になり、読みやすくなります。おそらく、エラーをよりよく見つけることができます。

于 2010-06-23T08:01:31.387 に答える