2014-01-21 56 views
0

請幫我解決這個問題,我已經有一個代碼,它運行完美,但事情是我需要確定每行的最高和最低。我不知道如何開始,請幫助我,也請向我解釋。這裏是代碼:確定陣列中最高和最低的每行

int [][] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}}; 
int row, col; 
for (row=0;row<3;row++){ 
    int sum = 0; 
    for (col=0;col<5;col++){  
     System.out.print(num[row][col]+"|"); 
     sum =sum+num[row][col]; 
    } 
    System.out.println("sum = "+sum); 
} 

回答

0

你可以做這樣的事情:

int [][] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}}; 
int row, col; 
for (row=0;row<3;row++){ 
colcount = 0; //count if is the first column 
mincol = 0; 
maxcol = 0; 
for (col=0;col<5;col++){  

    if(colcount == 0){ //if first time at the loop 
     mincol = num[row][col]; //mincol will be the first column 
     maxcol = num[row][col]; //maxcol will be the first column 
     colcount++; 
    }else{ 
     mincol = (mincol < num[row][col]) ? mincol : num[row][col]); //will verify if the mincol is really the lowest. If true, will maintain the mincol value.. else .. will get the current column. 
     maxcol = (maxcol > num[row][col]) ? maxcol : num[row][col]); //same of mincol, but using maxcol. 
    } 
} 

}

你可以把最大值和最小值山坳到一個數組..這是給你的。

希望它有幫助。

2

它真的和你已經有的相似。

  1. 創建變量來保存的,而不是你的summinmax
  2. 將行中的第一項分配到minmax(因爲這是迄今爲止最高和最低的數字)。
  3. 對於第一個之後的每個新號碼,檢查它是否低於min或高於max,並且在那種情況下保存。
+2

沒有勺子餵養 - 折舊 –

0

嘗試:

int [][] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}}; 
    for (int[] row : num) { 
     int lowest=row[0], highest=row[0]; 
     for (int i : row) { 
      if (i<lowest) { 
       lowest=i; 
      } 
      if (i>highest) { 
       highest=i; 
      } 
     } 
     System.out.println("Lowest:"+lowest+"; Highest:"+highest); 
    } 

希望它能幫助!

+3

請不要讓SO代碼工廠。如果你給算法這麼簡單的問題,讓OP可以解決這個問題,我會非常感激。 –

+2

你是誰? – Gorbles

+0

非常感謝! – user3214454

0

在循環的外部創建lowhigh,因此可以在整個過程中(即稍後在程序中)訪問它們。

int low = -1; 
int high = -1; 

for(int n = 0; n < 3; n++) { 
    int[] temp = num[1]; 
    for(int m : temp) { 
     if(low == -1) { 
      low = m; 
     } 

     if(high == -1) { 
      high = m; 
     } 

     if(m < low) { 
      low = m; 
     } else if(m > high) { 
      high = m; 
     } 
    } 
} 

不知道是否可以進一步優化,但嘿。

+0

第一行之後,我第二次循環你的代碼將檢查以前的最高和最低值 – Lijo

0
public static void main(String args[]) { 
    int [][] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}}; 

int row, col; 

     for (row=0;row<3;row++){ 
      int sum = 0; 
      int high=0,low=999999; 
      for (col=0;col<5;col++){  
     System.out.print(num[row][col]+"|"); 
     sum =sum+num[row][col]; 
     if(num[row][col]>high) 
     { 
     high=num[row][col]; 
     } 
     if(num[row][col]<low) 
     { 
     low=num[row][col]; 
     } 
     } 
     System.out.println("sum = "+sum); 
      System.out.println("higest:"+high+" and lowest="+low); 
     } 
    } 

每次迭代只檢查每個項目,並將其存儲在兩個變量高和低。 爲每個循環我們比較值與高和低和變化。

0

試試這個....

  • 使用臨時數組排序每一行。
  • 使用該數組查找每行的最大值和最小值。 int {[] [] [] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}};}}

    int row, col;   
    for (row = 0; row < 3; row++) { 
        int temp[]=num[row];   
        int sum = 0; 
        for (col = 0; col < temp.length; col++) { 
         for(int j=0;j<temp.length;j++){ 
          if(temp[col]<temp[j]){ 
           int a=temp[col]; 
           temp[col]=temp[j]; 
           temp[j]=a;      
          } 
    
         } 
    
        } 
        System.out.println("ROW"+row+": min="+temp[0]+" max="+temp[temp.length-1]); 
    }