-2

我上Hackerrank解決一個問題,你可以閱讀導通https://www.hackerrank.com/challenges/journey-to-the-moon爲什麼這段代碼不適用於hackerrank?

輸出是不正確的使用下面的代碼

我已經實現在所有代碼所需的數據結構,並試圖創建的尺寸陣列連接的組件。

import java.io.*; 
import java.util.*; 



public class Solution { 
public static void main(String[] args) throws Exception{ 

    BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));  
    String[] temp = bfr.readLine().split(" "); 
    int N = Integer.parseInt(temp[0]); 
    int I = Integer.parseInt(temp[1]); 
    Solution sol=new Solution(); 
    Graph g = sol.new Graph(N); 


    for(int i = 0; i < I; i++){ 
     temp = bfr.readLine().split(" "); 
     int a = Integer.parseInt(temp[0]); 
     int b = Integer.parseInt(temp[1]); 
     g.addEdge(a,b); 
     // Store a and b in an appropriate data structure of your choice 
    } 
    CC ccg=sol.new CC(g); 
    int len=ccg.getComp(); 

    long combinations = 0; 
    for(int k=0;k<len;k++){ 
     if(k==0){ 
      combinations+=ccg.getNum(k); 
     }else{ 
      combinations*=ccg.getNum(k); 
     } 
    } 
    // Compute the final answer - the number of combinations 

    System.out.println(combinations); 
    } 

    class Graph{ 
     final int s; 
     Bag[] adj; 
     public Graph(int si){ 
      s=si; 
      adj=new Bag[s]; 
      for(int i=0;i<si;i++){ 
       adj[i]=new Bag(); 
      } 
     } 
     public void addEdge(int i,int j){ 
      adj[i].add(j); 
      adj[j].add(i); 
     } 

     public int graphSize(){ 
      return s; 
     } 

     public Iterable<Integer> adj(int v){ 
      return adj[v]; 
     } 
    } 
    class Bag implements Iterable<Integer>{ 

    Node first; 
     int size=0; 
     final class Node{ 
      int i; 
      Node next; 
     } 
     public void add(int x){ 
      Node old=first; 
      first=new Node(); 
      first.i=x; 
      first.next=old; 
      size++; 
     } 
     public int getSize(){ 
      return size; 
     } 
     public Iterator<Integer> iterator() { 
      // TODO Auto-generated method stub 
      return new ListIterator(); 
     } 

     public class ListIterator implements Iterator<Integer>{ 
      private Node current=first; 
      public boolean hasNext(){ 
       return current!=null; 
      } 
      public void remove(){} 
      public Integer next(){ 
       int i=current.i; 
       current=current.next; 
       return i; 
      } 
     } 

    } 

    class CC{ 
     private boolean[] marked; 
     private int[] id; 
     private int[] comp; 
     private int count=0; 

     public CC(Graph g){ 
      int i=g.graphSize(); 
      marked=new boolean[i]; 
      id=new int[i]; 
      comp=new int[i]; 
      for(int j=0;j<i;j++){ 
       comp[j]=0; 
      } 

      for(int v=0;v<i;v++){ 
       if(!marked[v]){ 
        dfs(g,v); 
        count++; 
       } 
       comp[count]=comp[count]+1; 
      } 

     } 
     public int getComp(){ 
      return count; 
     } 
     public int getNum(int i){ 
      return comp[i]; 
     } 
     private void dfs(Graph g,int v){ 
      marked[v]=true; 
      id[v]=count; 
      for(int w:g.adj[v]){ 
       if(!marked[w]){ 
        dfs(g,w); 
       } 
      } 
     } 
    } 




    } 
+1

要求我們回顧的代碼很多,要理解它如何在沒有任何解釋的情況下工作並不容易。我不認爲我會想要這樣做。 –

回答

3

我已經完成了一些測試程序。

在所有的運行過程中,您的程序都會打印0.雖然0在某些情況下是正確的輸出,但在我的所有情況下,這並不是正確的輸出。所以這可能是Hackerrank降低程序的一個原因。示例性輸入:

3 1 
0 2 

我的意思是這個描述與宇航員0和2從一個國家和宇航員1來從其他國家2個國家。預計產出:2。程序的實際輸出:0

(我已經編輯了這一段。)看起來,如果所有對具有A等於B,您將得到一個ArrayIndexOutOfBoundsException。例如:

1 1 
0 0 

我什麼也看不到在Hackerrank規則禁止A == B,所以我想你應該考慮到這一點。

正如我在評論中所說的,我沒有深入您的程序來理解它爲什麼表現得如我所描述的那樣;我只是在觀察並向你彙報。我將把調試留給你自己。

相關問題