최근 배열의 요소가 아나그램인지 확인하라는 질문을 받았습니다. 구현을 완료했지만 테스트를 실행하면 5 개의 테스트 케이스 중 1 개만 통과했습니다. 문제는 그들이 테스트가 무엇인지 알 수 없으므로 내가 실패한 것에 대해 정말로 확신 할 수 없다는 것입니다. 기본적으로 단어의 문자를 곱하고이 숫자를 배열에 추가하는 아래의 대답을 다시 작성했습니다. 그런 다음 한 배열의 숫자를 다른 배열의 숫자와 비교하여 동일하면 true를 인쇄합니다. 기본적으로이 시나리오가 실패 할 수있는 시나리오는 무엇이며 어떻게 이러한 코드를 수정하여 이러한 사례를 설명 할 수 있습니까? 내가 main 메소드에서 실행 내 위의 예에서두 단어가 아나그램인지 확인합니다.
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;
}
}
}
,이 인쇄 참 참 거짓 ASCII 코드를 곱하여의 아이디어는 나쁘지 않다, 그러나 완벽하지
나는 그 코드가 무엇을하고 있는지 알지 못합니다. –
퀴즈에서이 코드에 대해 교수님이나 TA에게 이야기해야합니다. 그들은 왜 당신이 문제가 있는지 알고 있어야합니다. 또한, 내가 대학에있을 때, 우리는 "손 실행"을 가르쳤다. 이 코드를 손으로 직접 다른 입력으로 실행하고 오류가있는 위치를 찾으십시오. – markspace
'int'가 오버플로하기 때문에 약 5 자 이상의 단어는 실패하는 경향이 있습니다. 또한 편지의 두 가지 콤보가 동일한 제품을 생산할 수 없다는 것은 확실하지 않습니다. –