2010-06-23 60 views
0

我附上了一個代碼,它給出了基於cout語句的奇怪輸出。這個程序基本上計算Knuth的排列。Knuth置換算法奇怪的行爲

輸入是說:RUN1 代碼爲第一遍運行良好: 呼叫跟蹤將是:
[R UN1
烏爾N1
淖爾1
1nur
n1ur
nu1r
nur1
在這段代碼運行之後,呼叫正確返回到步驟
urn 1
在那裏,但它沒有在「R ETURN「聲明。

此外,如果是假設,其中置換完成循環內的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)); 
} 

謝謝!

+1

更好地描述它應該輸出什麼以及它真正輸出的內容會很有用。 – 2010-06-23 07:30:12

+6

請提供有關您所看到的行爲以及您的實際期望的信息。就目前而言,這不是一個問題,可能會被關閉(「請查看我的代碼」不是問題,對不起)。 – 2010-06-23 07:33:02

+0

輸入是說: RUN1 代碼爲第一遍運行良好: 呼叫跟蹤將是: [R UN1 烏爾N1 淖爾1 1nur 1nur n1ur nu1r nur1 最後一集後,調用返回正確的步驟,其中 urn 1在那裏,但它不處理「RETURN」語句下方的內容。 – RMR 2010-06-23 07:42:16

回答

1

使用像gdb這樣的調試器,逐行執行程序,同時檢查值。

1

對於C++,您應該在<string>標頭中使用string類。這將使你的代碼更安全,更好的可讀性。也許你可以更好地發現你的錯誤。

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; 
} 
+1

如果你想以「run1」開始和結束,不要檢查next_permutation是否返回false,而是檢查起始值。即'do {....; next_permuation(s.begin(),s.end()); } while(s!=「run1」);' – MSalters 2010-07-28 08:54:13