2012-02-23 12 views
1

任何人都可以幫助我。我試圖計算某些數字出現在表格佈局行中的次數 - 換句話說,垂直和水平。我想在(4和5)之間填入一組數字,以使數字表示4只出現4次和5次,只出現兩次(水平垂直&),例如6 * 6 ..注意事項Android - 計算時間兩個數字出現在單元格號碼

我該如何處理這些問題?

public boolean hasRepeatedNumbers(int[] x) { 
     int[] y = new int[x.length]; 
      System.arraycopy(x, 0, y, 0, y.length); 
     Array.sort(y); 
     int i; 
     for (i = 1; i < y.length; i++) { 
      if (y[i] == y[i-1]) return true; 
     } 
    return false; 
    } 

private int[] calculateUsedCells(int x, int y) { 
    int c[] = new int[2]; 
    // horizontal 
    for (int i = 0; i < 2; i++) { 
     if (i == y) 
      continue; 
     int t = getCell(x, i); 
     if (t != 0) 
      c[t - 1] = t; 
    } 
} 

任何意見將是巨大的,謝謝。

回答

0

考慮取一個int數組並在單元格中的索引處增加數組中的元素,最後檢查數組中的值。你會得到每個數字出現的次數。

例:
數4已經出現在1,2,5,6個細胞

array[content of the cell]++; 

所以最終陣列[4]給出的4倍的數量出現。

+0

說實話 - 我該如何編寫代碼,你給出的答案對我來說不是很明顯。任何幫助將被讚賞 – mascourt 2012-02-23 18:15:06

相關問題