【发布时间】:2014-09-03 05:02:22
【问题描述】:
我最近参加了一个测验,要求我确定数组中的元素是否是字谜。我完成了一个实现,但在运行他们的测试时,我只通过了 5 个测试用例中的 1 个。问题是,他们不允许我看到测试是什么,所以我真的不确定我失败了什么。我在下面重新创建了我的答案,它基本上将一个单词中的字母相乘并将这个数字添加到一个数组中。然后它将一个数组中的数字与另一个数组中的数字进行比较,如果它们相同,则打印 true。我基本上是在问在哪些情况下会失败,我将如何修改此代码以解决这些情况?
public class anagramFinder {
public static void main (String[] args){
String[] listOne = new String[5];
listOne[0] = "hello";
listOne[1] = "lemon";
listOne[2] = "cheese";
listOne[3] = "man";
listOne[4] = "touch";
String[] listTwo = new String[5];
listTwo[0] = "olleh";
listTwo[1] = "melon";
listTwo[2] = "house";
listTwo[3] = "namer";
listTwo[4] = "tou";
isAnagram(listOne,listTwo);
}
public static void isAnagram(String[] firstWords, String[] secondWords){
int firstTotal = 1;
int secondTotal = 1;
int[] theFirstInts = new int[firstWords.length];
int[] theSecondInts = new int[secondWords.length];
for(int i = 0;i<firstWords.length;i++){
for(int j = 0;j<firstWords[i].length();j++){
firstTotal = firstTotal * firstWords[i].charAt(j);
}
theFirstInts[i] = firstTotal;
firstTotal = 1;
}
for(int i = 0;i<secondWords.length;i++){
for(int j = 0;j<secondWords[i].length();j++){
secondTotal = secondTotal * secondWords[i].charAt(j);
}
theSecondInts[i] = secondTotal;
secondTotal = 1;
}
for(int i=0;i<minimum(theFirstInts.length,theSecondInts.length);i++){
if(theFirstInts[i] == theSecondInts[i]){
System.out.println("True");
} else {
System.out.println("False");
}
}
}
public static int minimum(int number,int otherNumber){
if(number<otherNumber){
return number;
} else {
return otherNumber;
}
}
}
在上面我在 main 方法中运行的示例中,这会打印 True True False False False,这是正确的
【问题讨论】:
-
我不知道该代码应该做什么。
-
你真的应该和你的教授或助教谈谈这个代码(在测验中)。他们需要知道您为什么会遇到问题。另外,在我上大学的时候,我们被教导“手部处决”。手动运行此代码,输入一些不同的输入,然后尝试找出错误可能出在哪里。
-
任何超过 5 个字符的单词都会失败,因为
int会溢出。此外,尚不清楚您不能让两个不同的字母组合产生相同的产品。 -
并学习如何编写 cmets。在您的 cmets 中说明您在计算的每个步骤中知道是正确的。
-
嗯,这是一个网站上的测验,不是为了学校,所以我没有办法得到测试用例
标签: java arrays string char anagram