die mittlere von 5 Zahlen nur mit if und else finden

  • Themenstarter Themenstarter Guest
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
tfa hat gesagt.:
Wo ist das Problem?

Hier:

Titel: die mittlere von 5 Zahlen nur mit if und else finden.


Also ohne sort-Algorithmus, Array, List usw. Nur mit if abfragen. Wenn ich versuche muss ich irgendwie 180 mal abfragen. Geht das irgendwie schneller?
 
Also ich komm mit 10 if-Abfragen aus. Programmier einfach ein Bubble-Sort. (Sind Zuweisungen erlaubt?). Bei 5 Zahlen sind 4 Durchläufe nötig mit je 4, 3, 2 und 1 Vergleichen.
 
10 klingt vom Gefühl her recht wenig - aber vermutlich hast du wirklich nur die Abfragen gezählt, und nicht etwa die Vergleiche.... oder?

EDIT: Ach so - mit zuweisungen ... boah, Koffeinmangel 🙄
 
Man müsste halt die genaue Aufgabenstellung kennen, ob Zuweisungen erlaubt sind. Nur mit if wäre es sicher sehr viel mehr. Aber das ist ja langweilig.
 
Hiho,

wenn Zuweisungen erlaubt sind, kann man das verkürzen.

Beispiel:

Zahlen 100, 50, 13, 1, 9

Als erstes Subtrahiert man von jedem Element die anderen Elemente und addiert die Absolutwerte.

1. 100-50 = 50
2. 100-13 = 87
3. 100-1 = 99
4. 100-9 = 91
Summe1 = |1.| + |2.| + |3.| + |4.| = 327

die anderen ebenso:

Summe1 = 327
Summe2 = 177
Summe3 = 140
Summe4 = 168
Summe5 = 144

Der mittlere Wert ist nun die kleinste Summe.

bye Saxony
 
Code:
int[] f = new int[]{100, 50, 13, 1, 9};
int m = -1;
		
int sum1 = Math.abs(f[0] - f[1]) + Math.abs(f[0] - f[2]) + Math.abs(f[0] - f[3]) + Math.abs(f[0] - f[4]);
int sum2 = Math.abs(f[1] - f[0]) + Math.abs(f[1] - f[2]) + Math.abs(f[1] - f[3]) + Math.abs(f[1] - f[4]);
int sum3 = Math.abs(f[2] - f[0]) + Math.abs(f[2] - f[1]) + Math.abs(f[2] - f[3]) + Math.abs(f[2] - f[4]);
int sum4 = Math.abs(f[3] - f[0]) + Math.abs(f[3] - f[1]) + Math.abs(f[3] - f[2]) + Math.abs(f[3] - f[4]);
int sum5 = Math.abs(f[4] - f[0]) + Math.abs(f[4] - f[1]) + Math.abs(f[4] - f[2]) + Math.abs(f[4] - f[3]);
		
if ((sum1 <= sum2) && (sum1 <= sum3) && (sum1 <= sum4) && (sum1 <= sum5)) m = 0;
else if ((sum2 <= sum1) && (sum2 <= sum3) && (sum2 <= sum4) && (sum2 <= sum5)) m = 1;
else if ((sum3 <= sum1) && (sum3 <= sum2) && (sum3 <= sum4) && (sum3 <= sum5)) m = 2;
else if ((sum4 <= sum1) && (sum4 <= sum2) && (sum4 <= sum3) && (sum4 <= sum5)) m = 3;
else if ((sum5 <= sum1) && (sum5 <= sum2) && (sum5 <= sum3) && (sum5 <= sum4)) m = 4;
				
System.out.println(f[m]);

Achso wenn Math.abs nicht erlaubt ist, dann bei den Summen jeden Summanden ersetzen durch:

Code:
((f[0] - f[1]) < 0)?(-1)*(f[0]-f[1]):(f[0]-f[1])

Wenn Zuweisungen nicht erlaubt sind, kann man auch die Platzhalter sum1, sum2 usw. in den ifs zurücksubstituieren mit den Formeln der Zuweisung. 🙂

bye Saxony
 
Meine Lösung find ich einfacher

Code:
    private static void mittel(int a, int b, int c, int d, int e) {
        int h;

        if (a>b) { h=a; a=b; b=h; }
        if (b>c) { h=b; b=c; c=h; }
        if (c>d) { h=c; c=d; d=h; }
        if (d>e) { h=d; d=e; e=h; }

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

        if (a>b) { h=a; a=b; b=h; }
        if (b>c) { h=b; b=c; c=h; }

        if (a>b) { h=a; a=b; b=h; }

        System.out.println(c);
    }
 
um auch was beizutragen (edit: beim 'Zurücksubstituieren' der Summen auch ganz nützlich, edit2: geht allerdings gar nicht ohne int sum):

Code:
int[] f = new int[] {100, 50, 13, 1, 9};
int m = 0;
int sum = 0;

int sum1 = Math.abs(f[0] - f[1]) + Math.abs(f[0] - f[2]) + Math.abs(f[0] - f[3]) + Math.abs(f[0] - f[4]);
sum = sum1;
int sum2 = Math.abs(f[1] - f[0]) + Math.abs(f[1] - f[2]) + Math.abs(f[1] - f[3]) + Math.abs(f[1] - f[4]);
if (sum2 < sum) { m = 1; sum = sum2; }
int sum3 = Math.abs(f[2] - f[0]) + Math.abs(f[2] - f[1]) + Math.abs(f[2] - f[3]) + Math.abs(f[2] - f[4]);
if (sum3 < sum) { m = 2; sum = sum3; }
int sum4 = Math.abs(f[3] - f[0]) + Math.abs(f[3] - f[1]) + Math.abs(f[3] - f[2]) + Math.abs(f[3] - f[4]);
if (sum4 < sum) { m = 3; sum = sum4; }
int sum5 = Math.abs(f[4] - f[0]) + Math.abs(f[4] - f[1]) + Math.abs(f[4] - f[2]) + Math.abs(f[4] - f[3]);
if (sum5 < sum) { m = 4; }
System.out.println(f[m]);
 
Danke euch allen, ihr seid alle toll!

Saxony und tfa haben gute Lösüngen vorgeschlagen, genau wie ich mir vorgestellt habe.

Die anderen sind auch gut, aber kein array oder die Funktionen aus der klasse math erlaubt. Nur if und else abfragen.

Nun kann man das Thema abhaken, als Gast kann ich das nicht! ???:L
 
@Slater deine Methode funktioniert aber nicht immer!

Lustig sieht es erst aus, wenn man komplett auf Zuweisungen und Math.abs verzichtet.

Code:
int[] f = new int[] { 50, 200, 9, 100, 1 };

		if (((((f[0] - f[1]) < 0 ? (-1) * (f[0] - f[1]) : f[0] - f[1])
				+ ((f[0] - f[2]) < 0 ? (-1) * (f[0] - f[2]) : f[0] - f[2])
				+ ((f[0] - f[3]) < 0 ? (-1) * (f[0] - f[3]) : f[0] - f[3]) + ((f[0] - f[4]) < 0 ? (-1)
				* (f[0] - f[4])
				: f[0] - f[4])) <= (((f[1] - f[0]) < 0 ? (-1) * (f[1] - f[0])
				: f[1] - f[0])
				+ ((f[1] - f[2]) < 0 ? (-1) * (f[1] - f[2]) : f[1] - f[2])
				+ ((f[1] - f[3]) < 0 ? (-1) * (f[1] - f[3]) : f[1] - f[3]) + ((f[1] - f[4]) < 0 ? (-1)
				* (f[1] - f[4])
				: f[1] - f[4])))
				&& ((((f[0] - f[1]) < 0 ? (-1) * (f[0] - f[1]) : f[0] - f[1])
						+ ((f[0] - f[2]) < 0 ? (-1) * (f[0] - f[2]) : f[0]
								- f[2])
						+ ((f[0] - f[3]) < 0 ? (-1) * (f[0] - f[3]) : f[0]
								- f[3]) + ((f[0] - f[4]) < 0 ? (-1)
						* (f[0] - f[4]) : f[0] - f[4])) <= (((f[2] - f[0]) < 0 ? (-1)
						* (f[2] - f[0])
						: f[2] - f[0])
						+ ((f[2] - f[1]) < 0 ? (-1) * (f[2] - f[1]) : f[2]
								- f[1])
						+ ((f[2] - f[3]) < 0 ? (-1) * (f[2] - f[3]) : f[2]
								- f[3]) + ((f[2] - f[4]) < 0 ? (-1)
						* (f[2] - f[4]) : f[2] - f[4])))
				&& ((((f[0] - f[1]) < 0 ? (-1) * (f[0] - f[1]) : f[0] - f[1])
						+ ((f[0] - f[2]) < 0 ? (-1) * (f[0] - f[2]) : f[0]
								- f[2])
						+ ((f[0] - f[3]) < 0 ? (-1) * (f[0] - f[3]) : f[0]
								- f[3]) + ((f[0] - f[4]) < 0 ? (-1)
						* (f[0] - f[4]) : f[0] - f[4])) <= (((f[3] - f[0]) < 0 ? (-1)
						* (f[3] - f[0])
						: f[3] - f[0])
						+ ((f[3] - f[1]) < 0 ? (-1) * (f[3] - f[1]) : f[3]
								- f[1])
						+ ((f[3] - f[2]) < 0 ? (-1) * (f[3] - f[2]) : f[3]
								- f[2]) + ((f[3] - f[4]) < 0 ? (-1)
						* (f[3] - f[4]) : f[3] - f[4])))
				&& ((((f[0] - f[1]) < 0 ? (-1) * (f[0] - f[1]) : f[0] - f[1])
						+ ((f[0] - f[2]) < 0 ? (-1) * (f[0] - f[2]) : f[0]
								- f[2])
						+ ((f[0] - f[3]) < 0 ? (-1) * (f[0] - f[3]) : f[0]
								- f[3]) + ((f[0] - f[4]) < 0 ? (-1)
						* (f[0] - f[4]) : f[0] - f[4])) <= (((f[4] - f[0]) < 0 ? (-1)
						* (f[4] - f[0])
						: f[4] - f[0])
						+ ((f[4] - f[1]) < 0 ? (-1) * (f[4] - f[1]) : f[4]
								- f[1])
						+ ((f[4] - f[2]) < 0 ? (-1) * (f[4] - f[2]) : f[4]
								- f[2]) + ((f[4] - f[3]) < 0 ? (-1)
						* (f[4] - f[3]) : f[4] - f[3]))))
			System.out.println(f[0]);
		else if (((((f[1] - f[0]) < 0 ? (-1) * (f[1] - f[0]) : f[1] - f[0])
				+ ((f[1] - f[2]) < 0 ? (-1) * (f[1] - f[2]) : f[1] - f[2])
				+ ((f[1] - f[3]) < 0 ? (-1) * (f[1] - f[3]) : f[1] - f[3]) + ((f[1] - f[4]) < 0 ? (-1)
				* (f[1] - f[4])
				: f[1] - f[4])) <= (((f[0] - f[1]) < 0 ? (-1) * (f[0] - f[1])
				: f[0] - f[1])
				+ ((f[0] - f[2]) < 0 ? (-1) * (f[0] - f[2]) : f[0] - f[2])
				+ ((f[0] - f[3]) < 0 ? (-1) * (f[0] - f[3]) : f[0] - f[3]) + ((f[0] - f[4]) < 0 ? (-1)
				* (f[0] - f[4])
				: f[0] - f[4])))
				&& ((((f[1] - f[0]) < 0 ? (-1) * (f[1] - f[0]) : f[1] - f[0])
						+ ((f[1] - f[2]) < 0 ? (-1) * (f[1] - f[2]) : f[1]
								- f[2])
						+ ((f[1] - f[3]) < 0 ? (-1) * (f[1] - f[3]) : f[1]
								- f[3]) + ((f[1] - f[4]) < 0 ? (-1)
						* (f[1] - f[4]) : f[1] - f[4])) <= (((f[2] - f[0]) < 0 ? (-1)
						* (f[2] - f[0])
						: f[2] - f[0])
						+ ((f[2] - f[1]) < 0 ? (-1) * (f[2] - f[1]) : f[2]
								- f[1])
						+ ((f[2] - f[3]) < 0 ? (-1) * (f[2] - f[3]) : f[2]
								- f[3]) + ((f[2] - f[4]) < 0 ? (-1)
						* (f[2] - f[4]) : f[2] - f[4])))
				&& ((((f[1] - f[0]) < 0 ? (-1) * (f[1] - f[0]) : f[1] - f[0])
						+ ((f[1] - f[2]) < 0 ? (-1) * (f[1] - f[2]) : f[1]
								- f[2])
						+ ((f[1] - f[3]) < 0 ? (-1) * (f[1] - f[3]) : f[1]
								- f[3]) + ((f[1] - f[4]) < 0 ? (-1)
						* (f[1] - f[4]) : f[1] - f[4])) <= (((f[3] - f[0]) < 0 ? (-1)
						* (f[3] - f[0])
						: f[3] - f[0])
						+ ((f[3] - f[1]) < 0 ? (-1) * (f[3] - f[1]) : f[3]
								- f[1])
						+ ((f[3] - f[2]) < 0 ? (-1) * (f[3] - f[2]) : f[3]
								- f[2]) + ((f[3] - f[4]) < 0 ? (-1)
						* (f[3] - f[4]) : f[3] - f[4])))
				&& ((((f[1] - f[0]) < 0 ? (-1) * (f[1] - f[0]) : f[1] - f[0])
						+ ((f[1] - f[2]) < 0 ? (-1) * (f[1] - f[2]) : f[1]
								- f[2])
						+ ((f[1] - f[3]) < 0 ? (-1) * (f[1] - f[3]) : f[1]
								- f[3]) + ((f[1] - f[4]) < 0 ? (-1)
						* (f[1] - f[4]) : f[1] - f[4])) <= (((f[4] - f[0]) < 0 ? (-1)
						* (f[4] - f[0])
						: f[4] - f[0])
						+ ((f[4] - f[1]) < 0 ? (-1) * (f[4] - f[1]) : f[4]
								- f[1])
						+ ((f[4] - f[2]) < 0 ? (-1) * (f[4] - f[2]) : f[4]
								- f[2]) + ((f[4] - f[3]) < 0 ? (-1)
						* (f[4] - f[3]) : f[4] - f[3]))))
			System.out.println(f[1]);
		else if (((((f[2] - f[0]) < 0 ? (-1) * (f[2] - f[0]) : f[2] - f[0])
				+ ((f[2] - f[1]) < 0 ? (-1) * (f[2] - f[1]) : f[2] - f[1])
				+ ((f[2] - f[3]) < 0 ? (-1) * (f[2] - f[3]) : f[2] - f[3]) + ((f[2] - f[4]) < 0 ? (-1)
				* (f[2] - f[4])
				: f[2] - f[4])) <= (((f[0] - f[1]) < 0 ? (-1) * (f[0] - f[1])
				: f[0] - f[1])
				+ ((f[0] - f[2]) < 0 ? (-1) * (f[0] - f[2]) : f[0] - f[2])
				+ ((f[0] - f[3]) < 0 ? (-1) * (f[0] - f[3]) : f[0] - f[3]) + ((f[0] - f[4]) < 0 ? (-1)
				* (f[0] - f[4])
				: f[0] - f[4])))
				&& ((((f[2] - f[0]) < 0 ? (-1) * (f[2] - f[0]) : f[2] - f[0])
						+ ((f[2] - f[1]) < 0 ? (-1) * (f[2] - f[1]) : f[2]
								- f[1])
						+ ((f[2] - f[3]) < 0 ? (-1) * (f[2] - f[3]) : f[2]
								- f[3]) + ((f[2] - f[4]) < 0 ? (-1)
						* (f[2] - f[4]) : f[2] - f[4])) <= (((f[1] - f[0]) < 0 ? (-1)
						* (f[1] - f[0])
						: f[1] - f[0])
						+ ((f[1] - f[2]) < 0 ? (-1) * (f[1] - f[2]) : f[1]
								- f[2])
						+ ((f[1] - f[3]) < 0 ? (-1) * (f[1] - f[3]) : f[1]
								- f[3]) + ((f[1] - f[4]) < 0 ? (-1)
						* (f[1] - f[4]) : f[1] - f[4])))
				&& ((((f[2] - f[0]) < 0 ? (-1) * (f[2] - f[0]) : f[2] - f[0])
						+ ((f[2] - f[1]) < 0 ? (-1) * (f[2] - f[1]) : f[2]
								- f[1])
						+ ((f[2] - f[3]) < 0 ? (-1) * (f[2] - f[3]) : f[2]
								- f[3]) + ((f[2] - f[4]) < 0 ? (-1)
						* (f[2] - f[4]) : f[2] - f[4])) <= (((f[3] - f[0]) < 0 ? (-1)
						* (f[3] - f[0])
						: f[3] - f[0])
						+ ((f[3] - f[1]) < 0 ? (-1) * (f[3] - f[1]) : f[3]
								- f[1])
						+ ((f[3] - f[2]) < 0 ? (-1) * (f[3] - f[2]) : f[3]
								- f[2]) + ((f[3] - f[4]) < 0 ? (-1)
						* (f[3] - f[4]) : f[3] - f[4])))
				&& ((((f[2] - f[0]) < 0 ? (-1) * (f[2] - f[0]) : f[2] - f[0])
						+ ((f[2] - f[1]) < 0 ? (-1) * (f[2] - f[1]) : f[2]
								- f[1])
						+ ((f[2] - f[3]) < 0 ? (-1) * (f[2] - f[3]) : f[2]
								- f[3]) + ((f[2] - f[4]) < 0 ? (-1)
						* (f[2] - f[4]) : f[2] - f[4])) <= (((f[4] - f[0]) < 0 ? (-1)
						* (f[4] - f[0])
						: f[4] - f[0])
						+ ((f[4] - f[1]) < 0 ? (-1) * (f[4] - f[1]) : f[4]
								- f[1])
						+ ((f[4] - f[2]) < 0 ? (-1) * (f[4] - f[2]) : f[4]
								- f[2]) + ((f[4] - f[3]) < 0 ? (-1)
						* (f[4] - f[3]) : f[4] - f[3]))))
			System.out.println(f[2]);
		else if (((((f[3] - f[0]) < 0 ? (-1) * (f[3] - f[0]) : f[3] - f[0])
				+ ((f[3] - f[1]) < 0 ? (-1) * (f[3] - f[1]) : f[3] - f[1])
				+ ((f[3] - f[2]) < 0 ? (-1) * (f[3] - f[2]) : f[3] - f[2]) + ((f[3] - f[4]) < 0 ? (-1)
				* (f[3] - f[4])
				: f[3] - f[4])) <= (((f[0] - f[1]) < 0 ? (-1) * (f[0] - f[1])
				: f[0] - f[1])
				+ ((f[0] - f[2]) < 0 ? (-1) * (f[0] - f[2]) : f[0] - f[2])
				+ ((f[0] - f[3]) < 0 ? (-1) * (f[0] - f[3]) : f[0] - f[3]) + ((f[0] - f[4]) < 0 ? (-1)
				* (f[0] - f[4])
				: f[0] - f[4])))
				&& ((((f[3] - f[0]) < 0 ? (-1) * (f[3] - f[0]) : f[3] - f[0])
						+ ((f[3] - f[1]) < 0 ? (-1) * (f[3] - f[1]) : f[3]
								- f[1])
						+ ((f[3] - f[2]) < 0 ? (-1) * (f[3] - f[2]) : f[3]
								- f[2]) + ((f[3] - f[4]) < 0 ? (-1)
						* (f[3] - f[4]) : f[3] - f[4])) <= (((f[1] - f[0]) < 0 ? (-1)
						* (f[1] - f[0])
						: f[1] - f[0])
						+ ((f[1] - f[2]) < 0 ? (-1) * (f[1] - f[2]) : f[1]
								- f[2])
						+ ((f[1] - f[3]) < 0 ? (-1) * (f[1] - f[3]) : f[1]
								- f[3]) + ((f[1] - f[4]) < 0 ? (-1)
						* (f[1] - f[4]) : f[1] - f[4])))
				&& ((((f[3] - f[0]) < 0 ? (-1) * (f[3] - f[0]) : f[3] - f[0])
						+ ((f[3] - f[1]) < 0 ? (-1) * (f[3] - f[1]) : f[3]
								- f[1])
						+ ((f[3] - f[2]) < 0 ? (-1) * (f[3] - f[2]) : f[3]
								- f[2]) + ((f[3] - f[4]) < 0 ? (-1)
						* (f[3] - f[4]) : f[3] - f[4])) <= (((f[2] - f[0]) < 0 ? (-1)
						* (f[2] - f[0])
						: f[2] - f[0])
						+ ((f[2] - f[1]) < 0 ? (-1) * (f[2] - f[1]) : f[2]
								- f[1])
						+ ((f[2] - f[3]) < 0 ? (-1) * (f[2] - f[3]) : f[2]
								- f[3]) + ((f[2] - f[4]) < 0 ? (-1)
						* (f[2] - f[4]) : f[2] - f[4])))
				&& ((((f[3] - f[0]) < 0 ? (-1) * (f[3] - f[0]) : f[3] - f[0])
						+ ((f[3] - f[1]) < 0 ? (-1) * (f[3] - f[1]) : f[3]
								- f[1])
						+ ((f[3] - f[2]) < 0 ? (-1) * (f[3] - f[2]) : f[3]
								- f[2]) + ((f[3] - f[4]) < 0 ? (-1)
						* (f[3] - f[4]) : f[3] - f[4])) <= (((f[4] - f[0]) < 0 ? (-1)
						* (f[4] - f[0])
						: f[4] - f[0])
						+ ((f[4] - f[1]) < 0 ? (-1) * (f[4] - f[1]) : f[4]
								- f[1])
						+ ((f[4] - f[2]) < 0 ? (-1) * (f[4] - f[2]) : f[4]
								- f[2]) + ((f[4] - f[3]) < 0 ? (-1)
						* (f[4] - f[3]) : f[4] - f[3]))))
			System.out.println(f[3]);
		else if (((((f[4] - f[0]) < 0 ? (-1) * (f[4] - f[0]) : f[4] - f[0])
				+ ((f[4] - f[1]) < 0 ? (-1) * (f[4] - f[1]) : f[4] - f[1])
				+ ((f[4] - f[2]) < 0 ? (-1) * (f[4] - f[2]) : f[4] - f[2]) + ((f[4] - f[3]) < 0 ? (-1)
				* (f[4] - f[3])
				: f[4] - f[3])) <= (((f[0] - f[1]) < 0 ? (-1) * (f[0] - f[1])
				: f[0] - f[1])
				+ ((f[0] - f[2]) < 0 ? (-1) * (f[0] - f[2]) : f[0] - f[2])
				+ ((f[0] - f[3]) < 0 ? (-1) * (f[0] - f[3]) : f[0] - f[3]) + ((f[0] - f[4]) < 0 ? (-1)
				* (f[0] - f[4])
				: f[0] - f[4])))
				&& ((((f[4] - f[0]) < 0 ? (-1) * (f[4] - f[0]) : f[4] - f[0])
						+ ((f[4] - f[1]) < 0 ? (-1) * (f[4] - f[1]) : f[4]
								- f[1])
						+ ((f[4] - f[2]) < 0 ? (-1) * (f[4] - f[2]) : f[4]
								- f[2]) + ((f[4] - f[3]) < 0 ? (-1)
						* (f[4] - f[3]) : f[4] - f[3])) <= (((f[1] - f[0]) < 0 ? (-1)
						* (f[1] - f[0])
						: f[1] - f[0])
						+ ((f[1] - f[2]) < 0 ? (-1) * (f[1] - f[2]) : f[1]
								- f[2])
						+ ((f[1] - f[3]) < 0 ? (-1) * (f[1] - f[3]) : f[1]
								- f[3]) + ((f[1] - f[4]) < 0 ? (-1)
						* (f[1] - f[4]) : f[1] - f[4])))
				&& ((((f[4] - f[0]) < 0 ? (-1) * (f[4] - f[0]) : f[4] - f[0])
						+ ((f[4] - f[1]) < 0 ? (-1) * (f[4] - f[1]) : f[4]
								- f[1])
						+ ((f[4] - f[2]) < 0 ? (-1) * (f[4] - f[2]) : f[4]
								- f[2]) + ((f[4] - f[3]) < 0 ? (-1)
						* (f[4] - f[3]) : f[4] - f[3])) <= (((f[2] - f[0]) < 0 ? (-1)
						* (f[2] - f[0])
						: f[2] - f[0])
						+ ((f[2] - f[1]) < 0 ? (-1) * (f[2] - f[1]) : f[2]
								- f[1])
						+ ((f[2] - f[3]) < 0 ? (-1) * (f[2] - f[3]) : f[2]
								- f[3]) + ((f[2] - f[4]) < 0 ? (-1)
						* (f[2] - f[4]) : f[2] - f[4])))
				&& ((((f[4] - f[0]) < 0 ? (-1) * (f[4] - f[0]) : f[4] - f[0])
						+ ((f[4] - f[1]) < 0 ? (-1) * (f[4] - f[1]) : f[4]
								- f[1])
						+ ((f[4] - f[2]) < 0 ? (-1) * (f[4] - f[2]) : f[4]
								- f[2]) + ((f[4] - f[3]) < 0 ? (-1)
						* (f[4] - f[3]) : f[4] - f[3])) <= (((f[3] - f[0]) < 0 ? (-1)
						* (f[3] - f[0])
						: f[3] - f[0])
						+ ((f[3] - f[1]) < 0 ? (-1) * (f[3] - f[1]) : f[3]
								- f[1])
						+ ((f[3] - f[2]) < 0 ? (-1) * (f[3] - f[2]) : f[3]
								- f[2]) + ((f[3] - f[4]) < 0 ? (-1)
						* (f[3] - f[4]) : f[3] - f[4])))) System.out.println(f[4]);;

Aber naja! 😀

bye Saxony
 
Code:
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
      int a=0;
      int b=55;
      int c=3;
      int d=7;     
      int e=6;
      
      
    	if(isMitte(a,b,c,d,e)) System.out.println("a ist Mitte");
    	if(isMitte(b,a,c,d,e)) System.out.println("b ist Mitte");
    	if(isMitte(c,b,a,d,e)) System.out.println("c ist Mitte");
    	if(isMitte(d,c,b,a,e)) System.out.println("d ist Mitte");
    	if(isMitte(e,d,c,b,a)) System.out.println("e ist Mitte");
	}
	
	public static boolean isMitte(int a, int b,int c, int d, int e)
	{
		 if(((a>b && a>c) && (a<d && a<e)) || 
	    		    ((a>b && a>d) && (a<c && a<e)) || 
	    		    ((a>b && a>e) && (a<c && a<d)) || 
	    		    ((a>c && a>d) && (a<b && a<e)) || 
	    		    ((a>c && a>e) && (a<b && a<d)) || 
	    		    ((a>d && a>e) && (a<b && a<c)))
			 return true;
		 else
			 return false;
		           
	}
}

:bae:
 
> @Slater deine Methode funktioniert aber nicht immer!

wo denn nicht?,
dachte ich hätte nur den sum-Block etwas abgekürzt
 
Hehe - weiß ich auch grad nicht mehr!

Ich werd hier ausserdem gleich bekloppt vor lauter Klammern.

[notiz mach]
Zuweisungen sind schon was feines!
[/notiz mach]

Also dann - ich mach erstma Strg+a -> Del, bevor ich ausraste.

🙂

bye Saxony
 
das Progamm von parabool findet doch die Mitte,
eine ähnliche Umsetzung:
Code:
public class Test
{
    public static void main(String[] args)
    {
        int a = 0;
        int b = 55;
        int c = 3;
        int d = 7;
        int e = 6;

        if (isMitte(a, b, c, d, e)) System.out.println("a ist Mitte");
        if (isMitte(b, a, c, d, e)) System.out.println("b ist Mitte");
        if (isMitte(c, b, a, d, e)) System.out.println("c ist Mitte");
        if (isMitte(d, c, b, a, e)) System.out.println("d ist Mitte");
        if (isMitte(e, d, c, b, a)) System.out.println("e ist Mitte");
    }

    public static boolean isMitte(int a, int b, int c, int d, int e)
    {
        int count = 0;
        count += Math.signum(a - b);
        count += Math.signum(a - c);
        count += Math.signum(a - d);
        count += Math.signum(a - e);
        return count == 0;
    }
}
 
Man kann durchaus noch einige überflüssige Operationen rausoptimieren.
Im Prinzip läuft der Algorithmus in zwei Phasen:
1. zwei Iterationen Bubblesort, danach interessieren uns das fünfte und vierte Element nicht mehr
2. Finden des größten Elements der ersten drei Elemente, da dieses in einer vollständig sortierten Liste an dritter Stelle stünde
Code:
public static int mittel(int a, int b, int c, int d, int e)
{ 
    int h;

    // Phase 1
    if (a>b) { h=a; a=b; b=h; } 
    if (b>c) { h=b; b=c; c=h; } 
    if (c>d) { h=c; c=d; d=h; } 
    if (d>e) d=e; 

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

    // Phase 2
    if (a>b) b=a; 
    if (b>c) c=b; 

    return c;
}
 
Saxony hat gesagt.:
Lustig sieht es erst aus, wenn man komplett auf Zuweisungen und Math.abs verzichtet.

Code:
...

Aber naja! 😀

bye Saxony

Deine Menge an Freizeit möchte ich auch mal haben. :shock:
 
Ohoh,

mein erstes Posting unangemeldet! Damn! 😉

Naja ich wollte eigentlich noch sagen, dass ich bloss die Summen auf trinären Operator umgeschrieben habe (also Math.abs weg) und dann nen replace in den ifs gemacht habe!

Zeit = 6-8min

bye Saxony
 
Ach ja, solche Spielereien 😀

Einmal kurz
Code:
    static int mittel(int a,int b,int c,int d,int e){return x(a,b,c,d,e,3);}
    static int y(int a, int b, int c, int d, int e, int n){return x(a,b<c?b:c,b<c?c:b,d<e?d:e,d<e?e:d,n-1);}
    static int x(int a, int b, int c, int d, int e, int n){return n==0?c:y(a<b?a:b,a<b?b:a,c<d?c:d,c<d?d:c,e,n);}
und einmal komplett ""funktional"" ohne Zuweisungen und ohne arithmetische Operationen 😀
Code:
    static int mittel(int a,int b,int c,int d,int e){return u(a,b,c,d,e);}
    static int u(int a, int b, int c, int d, int e){return v(a<b?a:b, a<b?b:a, c<d?c:d, c<d?d:c, e);}
    static int v(int a, int b, int c, int d, int e){return w(a, b<c?b:c, b<c?c:b, d<e?d:e, d<e?e:d);}
    static int w(int a, int b, int c, int d, int e){return x(a<b?a:b, a<b?b:a, c<d?c:d, c<d?d:c, e);}
    static int x(int a, int b, int c, int d, int e){return y(a, b<c?b:c, b<c?c:b, d<e?d:e, d<e?e:d);}
    static int y(int a, int b, int c, int d, int e){return z(a<b?a:b, a<b?b:a, c<d?c:d, c<d?d:c, e);}
    static int z(int a, int b, int c, int d, int e){return c; }
... allerdings beide auch ohne if und else - von daher bin ich nichtmal sicher, ob das die Aufgabenstellung erfüllt... ???:L :wink:
 
Und, der Vollständigkeit halber, und in Anlehnung an Saxony's Post, mal alles ge-inlined
Code:
static int mittel(int a,int b,int c,int d,int e){return(((a<b?a:b)<((a<b?b:a)<(c
<d?c:d)?(a<b?b:a):(c<d?c:d))?((a<b?b:a)<(c<d?c:d)?(a<b?b:a):(c<d?c:d)):(a<b?a:b)
)<(((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a))<((c<d?d:c)<e?(c<d?d:c):e)?((a<b?b:a
)<(c<d?c:d)?(c<d?c:d):(a<b?b:a)):((c<d?d:c)<e?(c<d?d:c):e))?(((a<b?b:a)<(c<d?c:d
)?(c<d?c:d):(a<b?b:a))<((c<d?d:c)<e?(c<d?d:c):e)?((a<b?b:a)<(c<d?c:d)?(c<d?c:d):
(a<b?b:a)):((c<d?d:c)<e?(c<d?d:c):e)):((a<b?a:b)<((a<b?b:a)<(c<d?c:d)?(a<b?b:a):
(c<d?c:d))?((a<b?b:a)<(c<d?c:d)?(a<b?b:a):(c<d?c:d)):(a<b?a:b))) <((((a<b?b:a)<(
c<d?c:d)?(c<d?c:d):(a<b?b:a))<((c<d?d:c)<e?(c<d?d:c):e)?((c<d?d:c)<e?(c<d?d:c):e
):((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a)))<((c<d?d:c)<e?e:(c<d?d:c))?(((a<b?b:
a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a))<((c<d?d:c)<e?(c<d?d:c):e)?((c<d?d:c)<e?(c<d?d:
c):e):((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a))):((c<d?d:c)<e?e:(c<d?d:c)))?(((a
<b?a:b)<((a<b?b:a)<(c<d?c:d)?(a<b?b:a):(c<d?c:d))?((a<b?b:a)<(c<d?c:d)?(a<b?b:a)
:(c<d?c:d)):(a<b?a:b))<(((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a))<((c<d?d:c)<e?(
c<d?d:c):e)?((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a)):((c<d?d:c)<e?(c<d?d:c):e))
?(((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a))<((c<d?d:c)<e?(c<d?d:c):e)?((a<b?b:a)
<(c<d?c:d)?(c<d?c:d):(a<b?b:a)):((c<d?d:c)<e?(c<d?d:c):e)):((a<b?a:b)<((a<b?b:a)
<(c<d?c:d)?(a<b?b:a):(c<d?c:d))?((a<b?b:a)<(c<d?c:d)?(a<b?b:a):(c<d?c:d)):(a<b?a
:b))):((((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a))<((c<d?d:c)<e?(c<d?d:c):e)?((c<
d?d:c)<e?(c<d?d:c):e):((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a)))<((c<d?d:c)<e?e:
(c<d?d:c))?(((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a))<((c<d?d:c)<e?(c<d?d:c):e)?
((c<d?d:c)<e?(c<d?d:c):e):((a<b?b:a)<(c<d?c:d)?(c<d?c:d):(a<b?b:a))):((c<d?d:c)<
e?e:(c<d?d:c)));}
Hübsch.
 
Naja, die erste ist eben kurz, mit 8 Vergleichen im Code (d.h. 4, aber alle doppelt), und die anderen eher ... Spielerei.... es gab ja keine "Kriterien" ... natürlich sind andere Lösungen leichter nachzuvollziehen und effizienter, aber darum ging's hier ja nicht :wink: Was ist daran deiner Meinung nach scheisse?
 
@didjitalist
Ja im Endeffekt sind wir ja dann davon ausgegangen, dass keine Zuweisungen und keine andere Klassen erlaubt sind (z.B. Math).

Schreib doch auch einmal eine (deine) Lösung zu diesen Restriktionen! 😉

bye Saxony
 
Und nachdem du sie gepostet hast, erweitern wir die Aufgabenstellung auf "die mittlere von 7 Zahlen", und schauen, welche Lösung DANN schöner und einfacher ist :bae:
 
Hiho

Naja ich meinte ja nicht, dass man das dann immer noch zwingend über Funktionen lösen muss. Es reicht ja dazu eine Lösung ohne Zuweisungen und ohne fremde Klassen. Wenns geht nur ()?:; 😉

bye Saxony
 
Also die Ein-Ausdruck-Lösung für 7 Zahlen funktioniert noch.
Für n=9 ist der Quelltext ca. 640 kB groß, aber er kompiliert nicht mehr, da der Bytecode der Methode größe als 64kB wäre.
Für n=11 wäre der Sourcecode über 10MB groß, bei n>11 steigt mein Ruby mit Out-Of-Memory aus :wink:
 
Dann hier nochmal die kurze Lösung für 7 Variablen
Code:
    static int mittel(int a,int b,int c,int d,int e, int f, int g){return x(a,b,c,d,e,f,g,4);}
    static int y(int a, int b, int c, int d, int e, int f, int g, int n){return x(a,b<c?b:c,b<c?c:b,d<e?d:e,d<e?e:d,f<g?f:g,f<g?g:f,n-1);}
    static int x(int a, int b, int c, int d, int e, int f, int g, int n){return n==0?c:y(a<b?a:b,a<b?b:a,c<d?c:d,c<d?d:c,e<f?e:f,e<f?f:e,g,n);}
🙂
 
Solche Snippets kann man in der Mittagspause oder während SVN-Updates schreiben ... den Anatz dafür zu finden hatte gestern Abend aber etwas länger gedauert....
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben