我想要有一些數字的排列,但是當我運行它不起作用。進程退出返回值`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,分段故障」,所以它必須是一個索引 - 問題,對吧? (發現錯誤在同一行,因爲我以爲)
當你使用調試器來瀏覽程序時,你發現了什麼? – 2014-11-23 15:46:51
你應該使用'char avnmbrs [11] =「1234567890」;'因爲你的數組中缺少'\ 0',所以''能夠使用'strlen'。 – Jarod42 2014-11-23 15:50:22
你應該使用'std :: vector'或'std :: array'(和它們的'size()'方法,而不是容易出錯的'sizeof')。 – Jarod42 2014-11-23 15:53:23