在Java算法中需要帮助以找到最大值和最小值

时间:2018-11-08 07:30:25

标签: java algorithm

我需要编写一个Java算法,该算法从int类型接收5个数字,并通过条件交换仅使用6次来打印最大值和最小值:

If (x>y) {
    Int tmp = x;
    x=y;
    y=x;
} 

问题是我只能通过7个条件交换而不是6个条件交换来执行此算法。有人可以帮助我了解我所缺少的内容吗?

    Scanner myScanner = new Scanner(System.in);
    int a = myScanner.nextInt();
    int b = myScanner.nextInt();
    int c = myScanner.nextInt();
    int d = myScanner.nextInt();
    int e = myScanner.nextInt();

    if(a>b)
    {
        int tmp = b;
        b = a;
        a = tmp;
    }

    if(a>c)
    {
        int tmp = c;
        c = a;
        a = tmp;
    }

    if(a>d)
    {
        int tmp = d;
        d = a;
        a = tmp;
    }

    if(a>e)
    {
        int tmp = e;
        e = a;
        a = tmp;
    }

    if(b>e)
    {
        int tmp = e;
        e = b;
        b = tmp;
    }

    if(c>e)
    {
        int tmp = e;
        e = c;
        c = tmp;
    }

    if(d>e)
    {
        int tmp = e;
        e = d;
        d = tmp;
    }

    System.out.println(a);
    System.out.println(e);

4 个答案:

答案 0 :(得分:3)

像合并排序那样思考:将问题分解为找到数字对的最小值和最大值;然后是成对的等。

if (a >= b) swap(a, b); // logically swap them; you can't actually write a method to do this.

现在a = min(a, b)b = max(a, b)

if (c >= d) swap(c, d);

现在c = min(c, d)d = max(c, d)

if (a >= c) swap(a, c);
if (b >= d) swap(b, d);

现在a = min(a, b, c, d)d = max(a, b, c, d)

然后,只需处理e

if (a >= e) swap(a, e);
if (d >= e) swap(d, e);

最小值在a中;最大值在e中。

答案 1 :(得分:0)

首先比较两对,然后就可以保存比较,因为知道第一对较小的必须与第二对较小的比较,而第一对必须与第二对较大的比较。剩下的数字必须与当前越来越小的数字进行比较,以防它适合那里。

实施:

Scanner myScanner = new Scanner(System.in);
int a = myScanner.nextInt();
int b = myScanner.nextInt();
int c = myScanner.nextInt();
int d = myScanner.nextInt();
int e = myScanner.nextInt();

myScanner.close();

// First pair
if(a>e)
{
    int tmp = e;
    e = a;
    a = tmp;
}

// Second pair
if(c>d)
{
    int tmp = d;
    d = c;
    c = tmp;
}

// Smaller
if(a>c) {
    int tmp = a;
    a = c;
    c = tmp;
}

// Bigger
if(d>e) {
    int tmp = d;
    d = e;
    e = tmp;
}

// Remaining value bigger
if(b>e) {
    int tmp = b;
    b = e;
    e = tmp;
}

// Remaining value smaller     
if(a>b) {
    int tmp = b;
    b = a;
    a = tmp;
}



System.out.println(a);
System.out.println(e);

答案 2 :(得分:0)

//成对比较您的数字,我将a-b和c-d配对,并且e将分别进行比较

Scanner myScanner = new Scanner(System.in);

int a = myScanner.nextInt();
int b = myScanner.nextInt();
int c = myScanner.nextInt();
int d = myScanner.nextInt();
int e = myScanner.nextInt(); 

int min ; int max ;
int min1 = a, max1 = b;
int min2 = c, max2 = d ; 

if(a > b ){
      min1 = b ;
      max1 = a ;
      // Here I set original min and max to min1 and max2 to save extra conditionals
      min = min1 ; 
      max = max1 ;  
}

if(c > d ){
      min2 = b ;
      max2 = a ; 
}

if(min1 > min2)
      min = min2 ; 
if(max2 > max1)
      max = max2 ;

// One last comparison for e and we've found what were looking for :)
if( e > max){ 
      max = e ; }
if( e < min){
      min = e ; }

答案 3 :(得分:-1)

将每个元素放入数组(arr1),然后导入集合和数组

import java.util.Arrays;
import java.util.Collections; 

int min = Collections.min(Arrays.asList(arr1));  // arr1 will be your array
int max = Collections.max(Arrays.asList(arr1));