2013-03-19 130 views
0

如何在Java中只使用2 for for的矩陣中找到列的最大值?矩陣中列的最大值?

for(int i = 1; i< N; i++) 
    for(int j = 1; j < M; j++) 
     i want to find the maxim for each column 
+1

爲什麼你需要2個循環來查找列中的最大值? – smk 2013-03-19 17:58:35

+0

我還能怎麼做? – George 2013-03-19 18:00:24

+0

也許他是指任何列中的最大值? – 2013-03-19 18:00:28

回答

1
public int findMaxInCol(int colIndex){ 
    int max = Integer.Min; 
    for(int row=0;row<Matrix.Rows;row++){ 

    if(matrix[row][colIndex] > max){ 
     max = matrix[row]colIndex]; 
    } 

} 
    return max; 
} 



void int findMaxOfMaxes() { 
    int maxOfMaxs = Integer.min; 
    for(int col=0;col<j;col++){ 
     int maxInCol = findMaxInCol(col); 
     if(maxInCol > maxOfMaxs) 
      maxOfMaxs = maxInCol; 
    } 
    return maxOfMaxs 

} 

//僞

//發現你在一個矩陣希望最大後編輯。你是對的,你需要2個循環。

+0

只有1可以做,但醜陋。只有當某人特意告訴你「你只能使用一個for循環」時,你纔會這樣做,然後它基本上涉及迭代索引並分別通過/和%將索引分解爲行和列。 – dspyz 2013-03-19 19:51:09

+0

這也取決於你的矩陣是如何定義的。例如,如果它被定義爲一個動態數組,那麼一個循環就足夠了:'for(int i = 0; i 2013-03-20 13:44:18