【发布时间】:2014-12-07 17:36:41
【问题描述】:
我还是个编程新手,所以请容忍我糟糕的语法和逻辑,如果有任何不好的话。我一直在用 C 编写一个密码解算器。它从用户那里读取 3 个单词,计算它们的值并将它们打印到屏幕上。 (例如send+more=money -> 9567+1085=10652)
我尝试了类似于排列算法的东西。它可以进行计算,但对于某些输入,结果会被打印多次:
如何修改我的代码,以便在第一次处理 if (n1+n2==n3) 下的 printf 命令时递归结束并且程序返回到 main 函数?
/* Swaps the two elements of an array. */
void swap(int v[], int i, int j) {
int t;
t = v[i];
v[i] = v[j];
v[j] = t;
}
/* Solves the Cryptarithmetic puzzle. */
int solve(int v[], int n, int i, char s1[], char s2[], char s3[], char letters[]) {
int k, m, j, t = 0, power, n1 = 0, n2 = 0, n3 = 0;
if (i == n) {
/*....some codes that
* calculate the value of each input word.....*/
/*This part verifies the values and if they are correct, prints them to screen*/
if (n1 + n2 == n3) {
printf("found!\n");
printf("\n%s : %6d\n", s1, n1);
printf("%s : %6d\n", s2, n2);
printf("%s : %6d\n", s3, n3);
}
} else
for (j = i; j < n; j++) {
swap(v, i, j);
solve(v, n, i + 1, s1, s2, s3, letters);
swap(v, i, j);
}
}
【问题讨论】:
-
由于您将函数定义为
int类型,因此您需要return类型为int的值。返回1,如果没有其他信息表明成功,或者0,如果至少遇到错误。 -
@DavidC.Rankin 我尝试在
printf ("%s : %6d\n", s3 , n3);之后添加return 1;,但没有任何改变。 -
你把
return放在你需要terminate递归的地方。比如i==n之后会发生什么,或者你完成else for循环之后会发生什么?请注意,在递归函数中的return之后,该函数仍将像backs out of the levels of recursion一样向后迭代。你怎么知道你什么时候完成处理?把退货放在那里...
标签: c recursion cryptarithmetic-puzzle