进程退出并返回值为“3221225477” - 比较字符或next_permutation`?

时间:2014-11-23 15:43:26

标签: c++ compare 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

顺便说一下。 string eq[] = {"ZWEI", "+", "VIER", "=", "NEUN"};

调试器说“程序收到信号SIGSEGV,分段错误。”,所以它必须是索引问题,对吧? (在我想到的同一行找到错误)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

因为数组包含不同大小的元素,所以在这种情况下你不能使用sizeof函数。一种可能的解决方案:在数组的末尾添加一个“\ 0”元素并实现自己的数组长度函数:

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

此函数将返回数组中没有“\ 0”元素的元素数量(这可能是您所期望的)