標題幾乎總結了我的問題。如何在不使用數組的情況下找到五個用戶輸入數字的中位數?如何在不使用數組的情況下查找五個用戶輸入數字的中位數?
我能想到做到這一點的唯一方法是使用多個if語句。 即使那樣,我也不確定從哪裏開始。
任何幫助或建議,將不勝感激。
標題幾乎總結了我的問題。如何在不使用數組的情況下找到五個用戶輸入數字的中位數?如何在不使用數組的情況下查找五個用戶輸入數字的中位數?
我能想到做到這一點的唯一方法是使用多個if語句。 即使那樣,我也不確定從哪裏開始。
任何幫助或建議,將不勝感激。
我喜歡使用多個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);
}
}
編輯:我簡化了一點,與我發佈的第一個版本相比。
我不知道爲什麼你會不想使用數組或列表,但這裏是你如何能做到這一點沒有:
的代碼只放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);
}
}
我無法得到它編譯,我不明白的邏輯。 –
@ OleV.V。你得到了什麼編譯錯誤?代碼只是將5個數字放入樹中,以便對它們進行排序。 for循環只是按升序排列數字。在奇怪的情況下,它是中間元素。即使是這樣,它也是兩個中間元素的平均值。 –
編輯幫助或我沒有得到所有的代碼粘貼到我的IDE。 –
您可以給該數字設置一個小於等於和大於等於的排名。如果兩個排名都是>=2
那麼你就知道這個數字在中間或等於中間的一個數字。
例如1 2 3 4 5
,價值3
有lhsRank=2
和rhsRank=2
另一個例子2 1 3 3 5
,價值3
有lhsRank=3
和rhsRank=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);
}
問題是你需要對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));
「不使用陣列」? – Teepeemm
使用'List'?你究竟想要做什麼? – azurefrog
要找到中位數,你必須保存它們的列表,所以如果你不打算使用一個數組,你將不得不使用某種'List' ...在這裏很乾淨和乾燥。 –