2016-09-29 48 views
0

标题几乎总结了我的问题。如何在不使用数组的情况下找到五个用户输入数字的中位数?如何在不使用数组的情况下查找五个用户输入数字的中位数?

我能想到做到这一点的唯一方法是使用多个if语句。 即使那样,我也不确定从哪里开始。

任何帮助或建议,将不胜感激。

+2

使用'List'?你究竟想要做什么? – azurefrog

+0

要找到中位数,你必须保存它们的列表,所以如果你不打算使用一个数组,你将不得不使用某种'List' ...在这里很干净和干燥。 –

回答

1

我喜欢使用多个if-else语句的想法 - 这既是因为它似乎符合问题的精神,因为挑战。我从this question中获得灵感,并写下以下内容。它提供了很多代码,所以它可能不是最好的解决方案,但证明它可以完成。

/** @return the median, that is, the 3rd greatest, of the arguments */ 
public static int median(int a, int b, int c, int d, int e) { 
    // sort a and b 
    int ab1, ab2; 
    if (a < b) { 
     ab1 = a; 
     ab2 = b; 
    } else { 
     ab1 = b; 
     ab2 = a; 
    } 
    assert ab1 <= ab2; 
    // c and d 
    int cd1, cd2; 
    if (c < d) { 
     cd1 = c; 
     cd2 = d; 
    } else { 
     cd1 = d; 
     cd2 = c; 
    } 
    assert cd1 <= cd2; 
    // from a through d find i1 <= i2 <= i3 and j <= i3 
    // however, discard i3 since it is too great to be the median 
    int i1, i2, j; 
    if (ab2 < cd2) { 
     i1 = ab1; 
     i2 = ab2; 
     j = cd1; 
    } else { 
     i1 = cd1; 
     i2 = cd2; 
     j = ab1; 
    } 
    assert i1 <= i2; 
    // we need to find the 2nd greatest of i1, i2, j and e 
    // sort j and e 
    int je1, je2; 
    if (j < e) { 
     je1 = j; 
     je2 = e; 
    } else { 
     je1 = e; 
     je2 = j; 
    } 
    assert je1 <= je2; 
    if (i2 < je2) { 
     // je2 is too great to be a median 
     // find the greates of i1, i2 and je1 
     // knowing that i1 <= i2 this can be accomplished by: 
     return Math.max(i2, je1); 
    } else { 
     // discard i2 as too great 
     return Math.max(i1, je2); 
    } 
} 

编辑:我简化了一点,与我发布的第一个版本相比。

-1

我不知道为什么你会不想使用数组或列表,但这里是你如何能做到这一点没有:

的代码只放5个号码为一棵树那么它们进行排序。 for循环只是按升序排列数字。在奇怪的情况下,它是中间元素。即使是这样,它也是两个中间元素的平均值。

import java.util.Iterator; 
import java.util.Scanner; 
import java.util.Set; 
import java.util.TreeSet; 

class Code 
{ 
    public static void main(String argss[]) 
    { 
     Scanner in = new Scanner(System.in); 

     System.out.println("Enter the 1st int:"); 
     int one = in.nextInt(); 

     System.out.println("Enter the 2nd int:"); 
     int two = in.nextInt(); 

     System.out.println("Enter the 3rd int:"); 
     int three = in.nextInt(); 

     System.out.println("Enter the 4th int:"); 
     int four = in.nextInt(); 

     System.out.println("Enter the 5th int:"); 
     int five = in.nextInt(); 

     Set<Integer> set = new TreeSet<>(); 
     set.add(one); 
     set.add(two); 
     set.add(three); 
     set.add(four); 
     set.add(five); 

     int size = set.size(); 
     double median = 0; 
     int mid = size/2; 

     if (size % 2 == 0) 
     { 
      int mid1 = 0; 
      int mid2 = 0; 

      int i = 0; 
      for (Iterator<Integer> iter = set.iterator(); iter.hasNext();) 
      { 
       int e = iter.next(); 
       if (i == mid - 1) 
       { 
        mid1 = e; 
       } 
       if (i == mid) 
       { 
        mid2 = e; 
       } 
       i++; 
      } 
      median = (mid1 + mid2)/2.0; 
     } 
     else 
     { 
      int i = 0; 
      for (Iterator<Integer> iter = set.iterator(); iter.hasNext();) 
      { 
       int e = iter.next(); 
       if (i == mid) 
       { 
        median = e; 
        break; 
       } 
       i++; 
      } 
     } 
     System.out.println("median = " + median); 
    } 
} 
+0

我无法得到它编译,我不明白的逻辑。 –

+0

@ OleV.V。你得到了什么编译错误?代码只是将5个数字放入树中,以便对它们进行排序。 for循环只是按升序排列数字。在奇怪的情况下,它是中间元素。即使是这样,它也是两个中间元素的平均值。 –

+0

编辑帮助或我没有得到所有的代码粘贴到我的IDE。 –

2

您可以给该数字设置一个小于等于和大于等于的排名。如果两个排名都是>=2那么你就知道这个数字在中间或等于中间的一个数字。

例如1 2 3 4 5,价值3lhsRank=2rhsRank=2

另一个例子2 1 3 3 5,价值3lhsRank=3rhsRank=2

int lhsRank(int sel, int a, int b, int c, int d) { 
    return (sel <= a ? 1 : 0) 
     + (sel <= b ? 1 : 0) 
     + (sel <= c ? 1 : 0) 
     + (sel <= d ? 1 : 0); 
} 
int rhsRank(int sel, int a, int b, int c, int d) { 
    return (sel >= a ? 1 : 0) 
     + (sel >= b ? 1 : 0) 
     + (sel >= c ? 1 : 0) 
     + (sel >= d ? 1 : 0); 
} 

然后你就可以像这样测试号:

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

if (lhsRank(a, b, c, d, e) >= 2 && rhsRank(a, b, c, d, e) >= 2) { 
    System.out.println(a); 
} else if (lhsRank(b, a, c, d, e) >= 2 && rhsRank(b, a, c, d, e) >= 2) { 
    System.out.println(b); 
} else if (lhsRank(c, a, b, d, e) >= 2 && rhsRank(c, a, b, d, e) >= 2) { 
    System.out.println(c); 
} else if (lhsRank(d, a, b, c, e) >= 2 && rhsRank(d, a, b, c, e) >= 2) { 
    System.out.println(d); 
} else if (lhsRank(e, a, b, c, d) >= 2 && rhsRank(e, a, b, c, d) >= 2) { 
    System.out.println(e); 
} 
+0

或者你可以把数字放在树中。 –

+1

@MichaelMarkidis我以为容器被禁止 – flakes

+0

OP刚才说的是数组。没有对树木说任何话。 –

1

问题是你需要对5个数字进行排序,然后选择第3个项目。

double one = 1,two = 2, three=3, four=4, five=5; 
double [] myarray = {one,two,three,four,five}; 
List<Double> mylist = new ArrayList<Double>(); 

for (double x : myarray) 
    mylist.add(x); 

Collections.sort(mylist); 
System.out.println("median " + mylist.get(2)); 
+1

“不使用阵列”? – Teepeemm

相关问题