使用循环获取数组中第二小的值,在已知最小值的情况下。

huangapple go评论67阅读模式
英文:

Getting the second lowest value in an array using only loops while already having the lowest value

问题

我正在尝试找出如何从用户输入的数组中获取第二小的值。

要获取最小的值,我已经这样做了:

int min = Integer.MAX_VALUE;

for (int i = 0; i < array.length; i++) {
    if (array[i] < min) {
        min = array[i];
    }
}

要获取第二小的值,我尝试了这样做:

int secMin = 0;

for (int i = 0; i < array.length - 1; i++) {
    if (array[i + 1] > array[i]) {
        secMin = array[i];
    }
}

输出不一致,有时是正确的,有时不是。我尝试过更改循环以及语句,但这只是猜测,我没有取得任何实质性的进展。有什么建议吗?

英文:

I am trying to figure out on how to get the second lowest value from an array that is input by the user.

To get the lowest value I've done this:

int min = Integer.MAX_VALUE;

for(int i = 0; i&lt;array.length; i++) {
		if( array[i] &lt; min ) {
			min = array[i];
		}
	}

To get the second lowest value I have tried this

int secMin = 0;
	
	for(int i = 0; i&lt;array.length-1; i++) {
		if(array[i+1] &gt; array[i]) {
			secMin = array[i];
		}
	}

The output is not consistent, meaning sometimes its right sometimes it isn't.I've tried changing the loop as well as the statements but its just guess work and I'm not making any real progress. Any tips?

答案1

得分: 2

如果您想继续将其作为两个单独的循环进行,只需对“min”值执行与寻找最小值相同的操作,但在查找时跳过“min”值。

// 寻找最小值
int min = Integer.MAX_VALUE;
for (int value : array) {
    if (value < min) {
        min = value;
    }
}
// 寻找第二小的值
int secMin = Integer.MAX_VALUE;
for (int value : array) {
    if (value != min) { // 跳过/忽略最小值
        if (value < secMin) {
            secMin = value;
        }
    }
}

您当然可以将其组合在一起:

// 寻找第二小的值
int secMin = Integer.MAX_VALUE;
for (int value : array)
    if (value != min && value < secMin)
        secMin = value;
英文:

If you want to keeping doing it as 2 separate loops, just do the same as for the min value, but skip the min value when looking.

// Find lowest value
int min = Integer.MAX_VALUE;
for (int value : array) {
    if (value &lt; min) {
        min = value;
    }
}
// Find second-lowest value
int secMin = Integer.MAX_VALUE;
for (int value : array) {
    if (value != min) { // Skip/ignore lowest value
        if (value &lt; secMin) {
            secMin = value;
        }
    }
}

You can of course combine that:

// Find second-lowest value
int secMin = Integer.MAX_VALUE;
for (int value : array)
    if (value != min &amp;&amp; value &lt; secMin)
        secMin = value;

答案2

得分: 1

你可以简单地更新你的第一个循环,同时寻找最小值和第二小值。试试这样做:

int min = Integer.MAX_VALUE;
int secmin = Integer.MAX_VALUE;

for (int i = 0; i < array.length; i++) {
    if (array[i] < min) {
        secmin = min; // 现在最小值变成了第二小值
        min = array[i]; // 当前值成为最小值
    } else if (array[i] < secmin) {
        // 如果值大于最小值但仍小于第二小值,则...
        secmin = array[i];
    }
}

我没有测试过,但大致按这种思路应该是可行的。

还有另一种方法在这里描述

英文:

You could simply update your first loop to look for both min and secMin at the same time. Try this:

int min = Integer.MAX_VALUE;
int secmin = Integer.MAX_VALUE;

for(int i = 0; i&lt;array.length; i++) {
        if( array[i] &lt; min ) {
            secmin = min; // the min is now second min
            min = array[i]; // the curent value becomes min                
        } else if ( array[i] &lt; secmin ){
            // if the value is greater than min but still less than second min then...
            secmin = array[i];
        } 
    }

I haven't tested it but something along those lines should work

There's another approach described here.

huangapple
  • 本文由 发表于 2020年9月23日 00:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64014306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定