2014-09-12 2 views
-5

이 할당에는 대문자로 구성된 문자열에 대한 추론이 포함됩니다. 동일한 클래스에 표시되는 여러 가지 정적 메서드를 구현합니다 (표시되지 않음). 여기에 세부 사항이 있습니다. 1. 첫 번째 메서드는 단일 문자열 매개 변수를 사용하고 해당 문자열의 스크램블 된 버전을 반환합니다. 스크램블 과정은 단어의 첫 글자에서 시작하여 왼쪽에서 오른쪽으로 계속됩니다. 두 개의 연속 문자가 "A"가 아닌 "A"가 아닌 문자로 구성되면 두 문자가 결과 문자열로 교체됩니다. 두 개의 인접한 위치에있는 문자가 바뀌면, 두 위치 중 어느 것도 미래의 스왑에 관련 될 수 없습니다. public static String scrambleWord (문자열 단어) 이 메서드는 주어진 단어 (빈 문자열 또는 대문자 만 포함하는 문자열)를 취하여 위에 제시된 규칙에 따라 단어의 스크램블 버전을 포함하는 문자열을 반환합니다. 다음 표는 몇 가지 단어 및 스크램블 버전의 예를 보여줍니다. 원래 단어 "TAN" "TNA" "아브라카 다 브라" "BARCADABARA" "와우" "와우" "땅 돼지" "ARADVRAK" "계란", "계란" "A" "A" "를 스크램블링 후 "" "ArrayList 할당에서 일부 문자를 스크램블합니다.

내가 사용되는 코드 만이 작동하지 복용량 여기

public class ScrambleWord { 

public static void main(String[] args) { 
    List<String> strList = new ArrayList<String>(); 
    strList.add("TAN"); 
    strList.add("ABRACADABRA"); 
    strList.add("WHOA"); 
    strList.add("EGGS"); 
    strList.add("A"); 
    strList.add(""); 
    System.out.prentln(MainMethod.scrambleWordMeth(strList)); 
} 

class MainMethod { 
    public static void scrambleWordMeth(List<String> strList) { 
     int curr = 0; 
     String res = ""; 
     while (curr < strList.size()) { 
      String currentString = strList.get(curr); 
      for(int i = 0; i < currentString.length(); i++){ 
       if (currentString.charAt(i) == 'A' && !(currentString.charAt(i + 1) == 'A')) { 
        res = res + currentString.substring(curr + 1, curr + 2); 
        res = res + 'A'; 
        curr = curr + 2; 

       } 
       else { 
        res = res + currentString.substring(curr, curr + 1); 
        curr++; 
       } 
      } 
      if (curr < strList.size()) { 
       res = res + currentString.charAt(curr); 
       //res=res + strList.substring(curr); 
      } 
     } 
     return res;  
    } 
} 
} 
+1

게시물을 수정하십시오. 그 _unreadable_ – Baby

+0

(위의 적절한 단락 사용에 큰 차이가 있습니다.) – user2864740

답변

1

입니다에 대한 템플릿은 어떻게 알고리즘이보다 명확하고 고립 된 방식으로 작업 할 수있는 방법 있도록 설정 ("여러 가지 방법"에 대한 작업 상태를 기록하십시오) . 이렇게하면 내부 루프의 curr (문자와 전혀 관련이없는)의 잘못된 사용과 같은 게시 된 코드의 일부 문제가 방지됩니다. 글자에 대한 배열의 사용은 분할 자체를 수행 할 필요없이 태스크 자체를 집중적으로 논리적으로 만듭니다.

static void scrambleAllWords(List<String> words) { 
    // Iterate through the list of word applying the scramble 
    // function and replacing the original item with the result. 
    for (int i = 0; i < words.size(); i++) { 
     String scrambled = scrambleWord(words.get(i)); 
     words.set(i, scrambled); 
    } 
} 

static String scrambleWord(String word) { 
    // Get the letters that make up the word 
    char[] letters = word.toCharArray(); 

    // Perform the algorithm on the letters 
    // for (int i = 0; i < .. 

    // Create a new string from the now-scrambled letters   
    return new String(letters); 
} 

알고리즘 자체는 비교적 간단하고 지금 분명히 다른 cruft에 이격 배열 같이 letters 적용 사소한되어야 다음의 의사 코드로 판독 될 수있다.

for i in the range [0, the number of letters in the word - 1) 
    if the letter at i is an 'A' and the following letter at i+1 is not an 'A' then 
     swap the letter at i with the letter at i+1 and 
     skip the next letter by advancing i by 1 
     (so that the current-now-next 'A' letter cannot be swapped again) 
    otherwise 
     do nothing 
+1

깔끔한, 의사 코드. – stealthjong

+0

내가 쓰고 싶은 것은 A 글자 뒤에 A 글자가 없거나 A 글자 중 마지막 글자가 없다는 것입니다. – Abdullah

+0

@ north.star 루프 범위를 좀 더 명확하게하기 위해 의사 코드를 약간 업데이트했습니다. 루프는'[0, letters.length - 1] 이상이어야합니다. 즉 단어에 8 개의 글자가 있으면 '[0, 7]'을 통해 i를 반복합니다 (i가 0..6이되도록). 이것은'letter [i]'가'letter '6''(마지막 문자로 'A'를 찾지도 않는다)와'letter [i + 1]'(2 번째 letter)는 대부분 편지의 문자임을 보장하는 'letter [7]'일 것입니다. – user2864740

관련 문제