我有点问题。我们必须实现几个Hashfunctions并使用键值打印出碰撞量和数组。一切正常,但控制台不会打印出我程序的最后三行。按Enter后,它会打印一个空白行,然后打印最后三行。问题是,计算机将纠正我的解决方案(这是正确的)。但由于计算机没有输入,他认为我错了。这真的很烦人。你们有什么想法吗?
这是我的代码(Java Eclipse)。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int testAmount = in.nextInt();
int m, n;
for (int cnt = 0; cnt < testAmount; cnt++) {
m = in.nextInt();
int linearSolution[] = new int[m];
int quadraticSolution[] = new int[m];
int doubleHashingSolution[] = new int[m];
n = in.nextInt();
// Scan the key and use Hashfunction to determine its place in the
// array
int linCol=0, quadCol=0, doubCol=0;
int j = 0;
for (int i = 0; i < n; i++) {
int key = in.nextInt();
// Lineares Sondieren
if (linearSolution[(key % m - j) % m] == 0) {
linearSolution[(key % m - j) % m] = key;
} else {
while (linearSolution[(key % m - j) % m] != 0) {
linCol++;
j++;
}
linearSolution[(key % m - j) % m] = key;
j = 0;
}
// Lineares Sondieren Ende
// Quadratisches Sondieren
if (quadraticSolution[(((((key%m)+m)%m)-((int)Math.pow((int)Math.ceil((double)j/2), 2)*(int)Math.pow(-1, j)))+m)%m] == 0) {
quadraticSolution[(((((key%m)+m)%m)-((int)Math.pow((int)Math.ceil((double)j/2), 2)*(int)Math.pow(-1, j)))+m)%m] = key;
} else {
while (quadraticSolution[(((((key%m)+m)%m)-((int)Math.pow((int)Math.ceil((double)j/2), 2)*(int)Math.pow(-1, j)))+m)%m] != 0) {
quadCol++;
j++;
}
quadraticSolution[(((((key%m)+m)%m)-((int)Math.pow((int)Math.ceil((double)j/2), 2)*(int)Math.pow(-1, j)))+m)%m] = key;
j = 0;
}
// Quadratisches Sondieren Ende
// Double Hashing
if (doubleHashingSolution[(((key%m)-(j*(1+(key%(m-2))))%m)+m)%m] == 0) {
doubleHashingSolution[(((key%m)-(j*(1+(key%(m-2))))%m)+m)%m] = key;
} else {
while (doubleHashingSolution[(((key%m)-(j*(1+(key%(m-2))))%m)+m)%m] != 0) {
doubCol++;
j++;
}
doubleHashingSolution[(((key%m)-(j*(1+(key%(m-2))))%m)+m)%m] = key;
j = 0;
}
// Double Hashing Ende
}
// Output
System.out.print(linCol+" ");
for (int ct = 0; ct < linearSolution.length; ct++) {
if (ct == linearSolution.length - 1) {
System.out.println(linearSolution[ct]);
} else
System.out.print(linearSolution[ct]+" ");
}
System.out.print(quadCol+" ");
for (int ct = 0; ct < quadraticSolution.length; ct++) {
if (ct == quadraticSolution.length - 1) {
System.out.println(quadraticSolution[ct]);
} else
System.out.print(quadraticSolution[ct]+" ");
}
System.out.print(doubCol+" ");
for (int ct = 0; ct < doubleHashingSolution.length; ct++) {
if (ct == doubleHashingSolution.length - 1) {
System.out.println(doubleHashingSolution[ct]);
} else
System.out.print(doubleHashingSolution[ct]+" ");
}
// Output Ende
}
}
}
这是我必须填入控制台的输入文件:
7
5 3 4 5 4
7 4 3 4 7 3
7 5 10 7 3 10 6
13 9 12 11 10 6 9 3 14 4 16
13 8 8 10 6 8 8 9 8 3
19 12 18 13 14 24 12 17 4 4 21 2 16 6
19 13 15 21 16 12 7 4 22 9 5 18 6 9 10
应该显示21行,但是没有按下输入它只是18.结果100%正确,我用解决方案和Diff Checker检查了它们。
在此先感谢您的帮助:)
乔纳森