2014-03-05 33 views
-3

我寫了一個關於找出具有相同數量的整數序列的最長子序列的代碼。用戶輸入一個以0結尾的整數序列,代碼完成剩下的工作,代碼只適用於小序列,我看不出錯誤的位置。這裏是我的代碼:查找列表中等效值的最長序列?

import java.util.*; 

public class test1 { 

    private static List<Integer> list = new ArrayList<>(); 
    private static int input; 
    private static int counter = 1; 

    public static void main(String[] args) { 

     Scanner scan = new Scanner(System.in); 
     System.out.print("Enter a series of numbers ending with 0: "); 

     boolean itsOk = true; 
     while (itsOk) { 
      input = scan.nextInt(); 
      list.add(input); 

      if (input == 0) 
       itsOk = false; 
     } 

     int index = 0; 
     for (int i = 1; i < list.size(); i++) 
      if (list.get(i).equals(list.get(i - 1))) { 
       counter++; 
       index = i - 2; 
      } 

     System.out.println("The longest same number sequence starts at index " 
       + index + " with " + counter + " values of " + list.get(index)); 

     Collections.sort(list); 
     System.out.println("\tThe sorted series of numbers is : " + list); 

    } 
} 
+11

'「爲什麼我的代碼不能很好地工作?」 - 就像旁白一樣,請努力使問題標題更具信息性。試着讓它總結你的問題,而不是你的沮喪。否則,你會增加我們的挫折感,這是不好的,因爲我們是志願者。 –

+1

至於你的問題本身,是時候用調試器進行一些調試了。在嘗試修復它之前,您必須先隔離並識別錯誤。 –

+0

您近了,但您的代碼有太多的邏輯錯誤,以便在此處找到明確的答案。我還建議更仔細地檢查您的邏輯,並使用調試器幫助逐步完成。順便提一下,當發現一系列數據中的最大值時,一般提示是跟蹤當前最大值並在遇到新的最大值時更新。 –

回答

0

您不記錄子序列組成的編號,也不記錄序列開始或其長度的索引。你需要跟蹤這些單獨的變量。

正如我在評論中所說,index = i - 2可以產生index == -1,所以這也是正確的。

此外,即使在更正這些語義錯誤之後,您的答案在技術上仍然不正確,因爲您在列表中包含終止0,因此請不要包含它。

private static List<Integer> list = new ArrayList<>(); 
private static int input; 
private static int counter = 1; 

public static void main(String[] args) { 

    Scanner scan = new Scanner(System.in); 
    System.out.print("Enter a series of numbers ending with 0: "); 

    while (true) { 
     input = scan.nextInt(); 
     if (input == 0) { 
      break; // don't add 0 to the list 
     } 
     list.add(input); 
    } 

    int index = 0;    // the current beginning of the sequence 
    int currNum;    // the current candidate for the sequence num 
    int theNum = list.get(0); // keep track of the num with the longest sequence 
    int theCount = 0;   // keep track of the count (length) of the longest sequence 
    int theIndex = 0;   // keep track of where the sequence began 
    for (int i = 1; i < list.size(); i++) { 
     currNum = list.get(i-1); 
     if (list.get(i).equals(currNum)) { 
      if (counter == 1) { 
       // note that a sequence is beginning, and its location 
       index = i - 1; 
      } 
      counter++; 
      if (counter > theCount) { 
       // check if you've found a longer sequence 
       theCount = counter; 
       theNum = currNum; 
       theIndex = index; 
      } 
     } else { 
      // sequence broken, count from scratch 
      counter = 1; 
     } 
    } 

    System.out.println("The longest same number sequence starts at index " 
      + theIndex + " with " + theCount + " values of " + theNum); 

    Collections.sort(list); 
    System.out.println("\tThe sorted series of numbers is : " + list); 

} 

查看在ideone上運行的代碼。

+0

清晰準確。感謝您的幫助和建議。 – Kurt16

0

你不能在這裏使用固定的值:

int index = 0; 
    for (int i = 1; i < list.size(); i++) 
     if (list.get(i).equals(list.get(i - 1))) { 
      counter++; 
      index = i - 2; 
     } 

index = i-2只有當序列正好是3個元素長的作品。

取而代之,您應該創建一個變量來存儲當前值之前最近看到的值,並將當前值與該值進行比較。此外,您應該創建一個變量來存儲當前序列的長度以及當前序列之前已經看到的最大序列的長度。如果當前序列長度超過先前的序列長度,只更新'索引'。

相關問題