【发布时间】:2013-03-16 23:45:28
【问题描述】:
我正在尝试学习 java,我在旧的 UIL 练习测试中遇到了这段代码,我无法弄清楚这部分代码在做什么。
d[i][j] = eval(d[i-1][j]+1
[i][j-1]+1, d[i-1][j-1] + cost); /* the commas confuse me, I don't know what to add to cost */
如果有帮助,这里是整个代码
private static int eval(int a, int b, int c) {
int m;
m = a;
if (b < m)
m = b;
if (c < m)
m = c;
return m;
}
public static int comp(String s, String t) {
int d[][];
int n, m, i, j;
char si, tj, cost;
n = s.length();
m = t.length();
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
d = < * 1 >;
for (i = 0; i <= n; i++) {
d[i][0] = i;
}
for (j = 0; j <= m; j++) {
d[0][j] = j;
}
for (i = 1; i <= n; i++) {
si = s.charAt(i - 1);
for (j = 1; j <= m; j++) {
tj = t.charAt(j - 1);
if (si == tj) {
cost = 0;
} else {
cost = 1;
}
d[i][j] = eval(d[i - 1][j] + 1,
d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
}
}
return d[n][m];
}
【问题讨论】:
-
请格式化您的代码。 (使用编辑器中的 { } 按钮。)
-
缺少一个逗号...
-
我建议你给你的方法和变量起有意义的名字。正如你所看到的,你很难弄清楚你的程序做了什么以及为什么使用通用名称。
-
多么好的方法名称
eval(),我想到了另一个eval()....我刚刚google了一下,如果eval()被添加到最新版本的Jdk中...... . :(