2014-11-23 58 views
-2

我想要有一些數字的排列,但是當我運行它不起作用。進程退出返回值`3221225477 - 比較字符或next_permutation`?


void bruteforce(string totry, string eq[]) { 
    if(count_string(totry) <= 9) { 
     char *avnmbrs = new char[10]; 
     strcpy(avnmbrs,"1234567890"); 

     string slv[count_string(totry)+1][2]; 

     string eqins = ""; 

     sort(avnmbrs, avnmbrs+strlen(avnmbrs)); 
     do { 
      for(int i = 0; i<=count_string(totry); i++) { 
       slv[i][0] = totry[i]; 
       slv[i][1] = avnmbrs[i]; 
      } 
      cout << "1"; 
      for(int i = 0; i<= (sizeof(eq)/sizeof(string)-1); i++) { 
       cout << "2"; 
       if(eq[i] != "+" && eq[i] != "-" && eq[i] != "*" && eq[i] != "/" && eq[i] != "=") { 
        for(int j = 0; j<=count_string(eq[i]); j++) { 
         cout << "3"; 
         for(int k = 0; k <= (sizeof(slv)/sizeof(string)-1); k++) { 
          cout << "4"; 
          if(eq[i][j] == slv[k][0][0]) { //here it seems to hang 
           eqins += slv[k][1]; 
           cout << "5"; 
          } 
          cout << "6"; 
         } 
        } 
       } else { 
        cout << "7"; 
        eqins += " "; 
        eqins += eq[i]; 
        eqins += " "; 
       } 

      } 
     } while (next_permutation(avnmbrs, avnmbrs+strlen(avnmbrs))); 

     cout << eqins; 
    } 
} 

它離開與返回值3221225477和輸出是12345444444444

Btw。 string eq[] = {"ZWEI", "+", "VIER", "=", "NEUN"};

調試器顯示「程序接收到的信號SIGSEGV,分段故障」,所以它必須是一個索引 - 問題,對吧? (發現錯誤在同一行,因爲我以爲)

+1

當你使用調試器來瀏覽程序時,你發現了什麼? – 2014-11-23 15:46:51

+1

你應該使用'char avnmbrs [11] =「1234567890」;'因爲你的數組中缺少'\ 0',所以''能夠使用'strlen'。 – Jarod42 2014-11-23 15:50:22

+0

你應該使用'std :: vector'或'std :: array'(和它們的'size()'方法,而不是容易出錯的'sizeof')。 – Jarod42 2014-11-23 15:53:23

回答

0

以下可能會有所幫助:

std::string get_alphabet(
    const std::string& n1, 
    const std::string& n2, 
    const std::string& sum) 
{ 
    std::string res = n1 + n2 + sum; 
    std::sort(res.begin(), res.end()); 
    res.resize(std::unique(res.begin(), res.end()) - res.begin()); 
    return res; 
} 

std::array<unsigned int, 256u> 
get_dico(const std::string& alpha, const int (&d)[10]) 
{ 
    std::array<unsigned int, 256u> res; 
    int i = 0; 
    for (auto c : alpha) { 
     res[c] = d[i++]; 
    } 
    return res; 
} 

unsigned int compute_word_value(
    const std::array<unsigned int, 256u>& dico, 
    const std::string& s) 
{ 
    unsigned int res = 0; 
    unsigned int factor = 1; 
    for (auto it = s.rbegin(); it != s.rend(); ++it) { 
     res += dico[*it] * factor; 
     factor *= 10; 
    } 
    return res; 
} 

void bruteforce_addition(
    const std::string& n1, 
    const std::string& n2, 
    const std::string& sum) 
{ 
    int d[10] = {0,1,2,3,4,5,6,7,8,9}; 
    const auto& alpha = get_alphabet(n1, n2, sum); 

    if (alpha.size() > 10) { 
     throw std::runtime_error("Too many letters"); 
    } 
    // those variable are used when alpha.size < 10 
    // to avoid to show duplicated results. 
    unsigned int last_v1 = 0; 
    unsigned int last_v2 = 0; 
    do { 
     const auto& dico = get_dico(alpha, d); 
     const unsigned int v1 = compute_word_value(dico, n1); 
     const unsigned int v2 = compute_word_value(dico, n2); 
     const unsigned int vsum = compute_word_value(dico, sum); 

     if (v1 + v2 == vsum 
      && (v1 != last_v1 || v2 != last_v2)) { 
      std::cout << v1 << " + " << v2 << " = " << vsum << std::endl; 
     } 
     last_v1 = v1; 
     last_v2 = v2; 
    } while (std::next_permutation(d, d + 10)); 
} 

Live example

+0

謝謝!我不會使用這段代碼,但你有一些很好的啓示。 – 2014-11-24 05:51:55

0

因爲數組包含大小不同的元素,你不能使用sizeof運算功能在這種情況下, 。一個可能的解決方案:你添加一個「\ 0」元素添加到數組的結尾,並實現自己的數組長度功能:

int alen(string a[]){ 
int size = 0; 
while(a[size]!="\0") size++; 
return size; 
} 

這個函數會返回數組中元素的個數沒有「\ 0」元素(這可能是你所期望的)

+0

錯字:'「\ 0」'應該是''\ 0'' – Jarod42 2014-11-24 07:47:24

+0

'strlen'確實已經有了。 (或'std :: string :: size') – Jarod42 2014-11-24 07:48:06