11.01.2013, 16:53 |
Der Verzweifelte |
Auf diesen Beitrag antworten » |
Knifflige Bruchrechnung, Zahlen vorhanden aber wohin damit
Meine Frage:
Ich verzweifle an folgender Aufgabe:
Ich habe die Zahlen 1, 3, 4, 6, 7, 12.
Die aufgabe: Füge alle Zahlen zu Brüchen zusamen so dass a/b+c/d=e/f
also 1/3+4/6=7/12 wenn ich die Zaheln in obiger Reihenfolge lasse. Allerdings habe ich momentan keine Idee wie ich zurLösung komme? Ihr etwa?
Danke Euch,
Stefan
Meine Ideen:
Die fehlt mir gerade |
Ich bin mal gemein:
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
|
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleTestSpace
{
class Program
{
public static void Main()
{
bool foundResult = false;
// Test all possible arrangements
while (!foundResult)
{
if (test(_pickedIndices))
foundResult = true;
else if (!pickNext())
break;
}
// Output result
if (foundResult)
Console.WriteLine(String.Format("a: {0}, b: {1}, c: {2}, d: {3}, e: {4}, f: {5}.",
_numbers[_pickedIndices[0]],
_numbers[_pickedIndices[1]],
_numbers[_pickedIndices[2]],
_numbers[_pickedIndices[3]],
_numbers[_pickedIndices[4]],
_numbers[_pickedIndices[5]]));
else Console.WriteLine("Impossible");
Console.ReadLine();
}
/// <summary>
/// Picks the next leaf of the decision tree of possible arrangements for indices
/// (six choices for first, five for second and so on)
/// </summary>
/// <returns>whether there was a next leaf</returns>
private static bool pickNext()
{
List<int> freeIndices = new List<int>();
// For all layers of the tree
for (int i = 5; i >= 0; i--)
{
// Haven't visited all of the node's children, visit next
if (_currentBranch[i] < 5 - i)
{
_currentBranch[i]++;
freeIndices.Add(_pickedIndices[i]);
// Pick smallest index larger than the last
// (we need to pick some order, even thoguh we dont really care which)
_pickedIndices[i] = freeIndices.Where((int item) => item > _pickedIndices[i]).Min();
freeIndices.Remove(_pickedIndices[i]);
// Set first branch
for (int u = i + 1; u < 6; u++)
{
_currentBranch[u] = 0;
// According to order, start with the smallest possible
_pickedIndices[u] = freeIndices.Min();
freeIndices.Remove(_pickedIndices[u]);
}
return true;
}
// Jump to next higher node and try again
else if (i != 0)
freeIndices.Add(_pickedIndices[i]);
else return false;
}
return false;
}
/// <summary>
/// Test whether an ordering of the numbers fulfills the equation,
/// done with precision of EPSILON
/// </summary>
/// <param name="attempt">Ordering of the numbers</param>
/// <returns>whether the equation was fulfilled</returns>
private static bool test(List<int> attempt)
{
return (Math.Abs(_numbers[attempt[0]] / _numbers[attempt[1]] + _numbers[attempt[2]] / _numbers[attempt[3]] -
_numbers[attempt[4]] / _numbers[attempt[5]]) <= EPSILON);
}
private static List<int> _currentBranch = new List<int>() { 0, 0, 0, 0, 0, 0 };
private static List<int> _pickedIndices = new List<int>() { 0, 1, 2, 3, 4, 5 };
private static List<double> _numbers = new List<double>() { 1d, 3d, 4d, 6d, 7d, 12d };
// Precision of float comparison
private const double EPSILON = 0.0001d;
}
} |
|
Oder um es anders auszudrücken, hier hilft vermutlich nur ausprobieren (Zahlen in die Formel einsetzen, solange bis eine Variation klappt). Obiger code ist ein Bruteforcer in C# (zugegeben kein besonders schöner), der für das gegebene Problem eine Lösung ausspuckt (er probiert die 6! = 720 Möglichkeiten solange durch, bis etwas klappt oder er am Ende ist). Wenn dir bekannt ist, wie man dies kompiliert, hast du deine Lösung. Ansonsten: probieren, probieren, probieren
PS: Vielleicht gibt es eine mir unbekannte Abkürzung, ich vermute aber eher nicht. |
Ich bezweifle, dass es dafür einen zielführenden, mathematischen Weg gibt.
Durch Probieren kommt man schnell zu: [ 1/6 + 7/12 = 3/4] |
Nur für Interessierte (die Aufgabe wurde ja eh bereits gelöst):
Quickperm ist eine weitaus effizientere Methode ist, Permutationen zu berechnen. |
11.01.2013, 23:38 |
Der Verzweifelte |
Auf diesen Beitrag antworten » |
Erst einmal vielen Dank für die Antworten.
@ Bakatan, ich sehe die Lösung allerdings nicht, lese ich etwas falsch???
Allerdings muss ich auch sagen daß ich kein Programmierer bin und das Skript nicht lesen kann |
Da ich ebenfalls keinen besonderen Lerneffekt bei "probiere wild herum" sehe kann ich Gualtiero's Spoiler etwas offensichtlicher machen: Er hat Schrift in der Farbe des Hintergrunds gewählt, damit man die Lösung nur liesst, wenn man wirklich will. |
12.01.2013, 10:09 |
Der Verzweifelte |
Auf diesen Beitrag antworten » |
Vielen Dank an alle, ihr habt meinen Tag gerettet. |