简体   繁体   English

确定数组中每行的最高和最低

[英]Determine the Highest and Lowest per row in array

Please help me on this, I already have a code and it runs perfectly but the thing is I need to determine the Highest and Lowest per row. 请帮助我,我已经有一个代码,它运行完美,但事情是我需要确定每行的最高和最低。 I don't know how to start, please help me and also please explain to me. 我不知道如何开始,请帮帮我,也请向我解释。 here is the code: 这是代码:

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); 
}

It is really similar to what you already have. 它与您已有的非常相似。

  1. Create variables to hold your min and max values instead of sum . 创建变量以保存minmax而不是sum
  2. Assign the first item in the row to both min and max (since that is the highest and lowest number so far). 将行中的第一项分配给minmax (因为这是迄今为止的最高和最低数字)。
  3. For each new number after the first, check if it is lower than min or higher than max , and in that case save that instead. 对于第一个之后的每个新数字,检查它是否低于min或高于max ,并且在这种情况下保存它。

Try: 尝试:

    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);
    }

Hope it helps! 希望能帮助到你!

Creating low and high outside of the loop so they can be accessed throughout (ie later on in the program). 在循环外创建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;
        }
    }
}

Not sure if it can be optimised further, but hey. 不确定它是否可以进一步优化,但是嘿。

 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);
        }
    }

on each iteration just check each item and store it in two variables high AND low. 在每次迭代时,只需检查每个项目并将其存储在高和低两个变量中。 for each loop we compare the values with high and low and change. 对于每个循环,我们将值与高和低进行比较并进行更改。

try this.... 尝试这个....

  • Use a temporary array to sort each row. 使用临时数组对每行进行排序。
  • Use that array to find the max and min values of each row. 使用该数组查找每行的最大值和最小值。

    int[][] num = {{1, 4, 3, 0, 5}, {2, 4, 7, 8, 10}, {3, 9, 60, 20, 4}}; 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]); } 

You can do something like this: 你可以这样做:

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.
    }
}

} }

you can put the max and min col into an array.. it's up to you. 你可以把max和min col放到一个数组中......这取决于你。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM