<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Algorithmus fuer Kombinationen]]></title><description><![CDATA[<p>Also ich brauche einen Algorithmus der mir alle moeglichen Kombinationen bis zu einem bestimmten Wert liefert!<br />
z.B. Der max Wert soll 11 sein und array mit Werten {4, 3, 5} soll folgendes liefern:<br />
4, 4, 3<br />
4, 3, 3<br />
3, 4, 4<br />
3, 3, 5<br />
3, 3, 3<br />
3, 4, 3<br />
5, 4<br />
5, 3, 3<br />
(Hoff ich hab alle)</p>
<p>Also bisher hab ich sowas:</p>
<pre><code class="language-cpp">int arr[4] = {2, 1, 6, 3};
	int value = 0;
	int tmpV = 0;

	for (int i=0; i&lt;c; i++) {
		value += arr[i];
		tmpV = value;
		std::cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot; &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;

		for (int k=0; k&lt;c; k++) {
			if ((value + arr[k]) &lt;= summationsLength) {
				value += arr[k];
				tmpV = value;
				std::cout &lt;&lt; arr[k] &lt;&lt; &quot; &quot; &lt;&lt; arr[k] &lt;&lt; &quot; &quot;;
			}

			for (int j=k; j&lt;c; j++) {
				if ((value + arr[j]) &lt;= summationsLength) {
					value += arr[j];
					std::cout &lt;&lt; arr[j] &lt;&lt; &quot; &quot; &lt;&lt; arr[j] &lt;&lt; &quot; &quot;;
				}
			}
			value = tmpV;
		}
		value = 0;
		std::cout &lt;&lt; std::endl;
	}
</code></pre>
<p>Leider funktioniert der nicht wirklich korrekt, weis vlt wer ne Loesung??</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/279408/algorithmus-fuer-kombinationen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 24 Aug 2026 13:29:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/279408.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 Dec 2010 12:20:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Sun, 26 Dec 2010 12:20:52 GMT]]></title><description><![CDATA[<p>Also ich brauche einen Algorithmus der mir alle moeglichen Kombinationen bis zu einem bestimmten Wert liefert!<br />
z.B. Der max Wert soll 11 sein und array mit Werten {4, 3, 5} soll folgendes liefern:<br />
4, 4, 3<br />
4, 3, 3<br />
3, 4, 4<br />
3, 3, 5<br />
3, 3, 3<br />
3, 4, 3<br />
5, 4<br />
5, 3, 3<br />
(Hoff ich hab alle)</p>
<p>Also bisher hab ich sowas:</p>
<pre><code class="language-cpp">int arr[4] = {2, 1, 6, 3};
	int value = 0;
	int tmpV = 0;

	for (int i=0; i&lt;c; i++) {
		value += arr[i];
		tmpV = value;
		std::cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot; &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;

		for (int k=0; k&lt;c; k++) {
			if ((value + arr[k]) &lt;= summationsLength) {
				value += arr[k];
				tmpV = value;
				std::cout &lt;&lt; arr[k] &lt;&lt; &quot; &quot; &lt;&lt; arr[k] &lt;&lt; &quot; &quot;;
			}

			for (int j=k; j&lt;c; j++) {
				if ((value + arr[j]) &lt;= summationsLength) {
					value += arr[j];
					std::cout &lt;&lt; arr[j] &lt;&lt; &quot; &quot; &lt;&lt; arr[j] &lt;&lt; &quot; &quot;;
				}
			}
			value = tmpV;
		}
		value = 0;
		std::cout &lt;&lt; std::endl;
	}
</code></pre>
<p>Leider funktioniert der nicht wirklich korrekt, weis vlt wer ne Loesung??</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1998946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1998946</guid><dc:creator><![CDATA[Typ222]]></dc:creator><pubDate>Sun, 26 Dec 2010 12:20:52 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Sun, 26 Dec 2010 13:51:08 GMT]]></title><description><![CDATA[<p><a href="http://www.cplusplus.com/reference/algorithm/next_permutation/" rel="nofollow">http://www.cplusplus.com/reference/algorithm/next_permutation/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1998970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1998970</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 26 Dec 2010 13:51:08 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Sun, 26 Dec 2010 14:02:49 GMT]]></title><description><![CDATA[<p>Aber dadurch wuerd ich die Ausgabe<br />
5,4<br />
nicht erhalten...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1998977</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1998977</guid><dc:creator><![CDATA[Typ222]]></dc:creator><pubDate>Sun, 26 Dec 2010 14:02:49 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Sun, 26 Dec 2010 16:36:43 GMT]]></title><description><![CDATA[<p>Typ222 schrieb:</p>
<blockquote>
<p>Aber dadurch wuerd ich die Ausgabe<br />
5,4<br />
nicht erhalten...</p>
</blockquote>
<p>Ehrlich gesagt verstehe ich auch nicht, wie die 5,4 bei dir ins Schema passt (oder nach welchem Schema dein Beispiel überhaupt aufgebaut ist). Wenn Leerstellen auch erlaubt sind, dann für deiner Grundmenge eben noch Leerstellen hinzu.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1999027</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1999027</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 26 Dec 2010 16:36:43 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Sun, 26 Dec 2010 17:44:40 GMT]]></title><description><![CDATA[<p>Es geht nicht um die Leerstellen sondern um die Ausgabe der Menge!<br />
Es sollten ALLE MOEGLICHEN Kombinationen ausgegeben werden die in Summe den Maximalwert (z.B. 11) ergeben!</p>
<p>P.S.<br />
Sorry hab ein Paar vergessen:<br />
4, 3, 4<br />
3, 3, 4<br />
5, 5</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1999047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1999047</guid><dc:creator><![CDATA[Typ222]]></dc:creator><pubDate>Sun, 26 Dec 2010 17:44:40 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Sun, 26 Dec 2010 17:46:34 GMT]]></title><description><![CDATA[<p>Das schreit nach Rekursion.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1999050</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1999050</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Sun, 26 Dec 2010 17:46:34 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Sun, 26 Dec 2010 18:03:09 GMT]]></title><description><![CDATA[<p>Hat da vlt wer ne Idee mit der Rekursion blick da im Moment nicht ganz durch...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1999057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1999057</guid><dc:creator><![CDATA[Typ222]]></dc:creator><pubDate>Sun, 26 Dec 2010 18:03:09 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Sun, 26 Dec 2010 19:23:07 GMT]]></title><description><![CDATA[<p>Hab kurz was gecoded.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using std::cout;
using std::endl;

const int Elements = 3; // Amount of elements
const int const values[] = {3, 4, 5}; // Values to build a combination
int combination[3] = {0}; // Temporary combination
int combination_count = 0;

// Print a combination
void print_combination()
{
	for( int i = 0; i &lt; Elements; i++ )
		cout &lt;&lt; combination[i];
	cout &lt;&lt; endl;
}

// Caluclates recursively all combinations of the array elements stored in 'values'. (Must start with 0).
void combinations( int position )
{
	// For each position loop through all possible elements
	for( int i = 0; i &lt; Elements; i++ )
	{
		combination[position] = values[i];

		// Last position reached? If so print and increase count
		if ( position == Elements - 1 )
		{
			print_combination();
			combination_count++;
		}
		// Move to the position further right in the array
		else
			combinations( position + 1 ); // Recursive call
	}
}

int main()
{
	combinations( 0 );
	cout &lt;&lt; endl &lt;&lt; &quot;Combinations: &quot; &lt;&lt; combination_count &lt;&lt; endl &lt;&lt; endl;
}
</code></pre>
<p>Gibt alle möglichen Kombination aus. Du kannst noch selber eine Funktion schreiben, die überprüft, dass die Sumem der Elemente kleiner als 11 ist.</p>
<p>Interessant sollte vor allem die Funktion combinations sein. Du kannst dann das ganze natürlich noch etwas flexibler programmieren. Im Moment ist alles ziemlich statisch.</p>
<p>PS:<br />
Du kannst mit dieser Methode beliebig grosse Mengen nehmen für die Kombinationen. Du musst einfach die Konstante und die Längen der beiden Arrays anpassen.</p>
<p>*Edit<br />
Sry wegen den englischen Kommentaren. Hoffe das macht dir nix aus. Habs aus Gewohnheit auf englisch gemacht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1999087</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1999087</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Sun, 26 Dec 2010 19:23:07 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus fuer Kombinationen on Tue, 28 Dec 2010 10:39:02 GMT]]></title><description><![CDATA[<p>Typ222 schrieb:</p>
<blockquote>
<p>Hat da vlt wer ne Idee mit der Rekursion blick da im Moment nicht ganz durch...</p>
</blockquote>
<p>Aus deiner Erklärung geht nicht ganz hervor, was für dich alle Kombinationen sind. Du sagst, dass der Maximalwert nicht erreicht werden muss, so dass z.B. 5, 4 gültig ist, obwohl die Summe nur 9 statt 1 ist. OK, also alle Kombinationen, deren Summe 11 nicht übersteigt. Aber warum ist dann 5 nicht in deiner Aufzählung dabei? Ich vermute, du suchst alle <strong>maximalen</strong> Kombinationen, deren Summe kleiner gleich einem bestimmten Maximalwert ist.</p>
<p>Ich hab die Idee mit der Rekursion hier mal ausprogrammiert:</p>
<pre><code class="language-cpp">void print_combinations_max(vector&lt;int&gt; const&amp; values, int start,
			    vector&lt;int&gt;&amp; work, int max_sum) {
  bool is_maximal = true;

  for (int i = start, n = values.size(); i &lt; n; ++i) {
    int x = values[i];
    if (x &lt;= max_sum) {
      is_maximal = false;
      work.push_back(x);
      print_combinations_max(values, start, work, max_sum - x);
      work.pop_back();
    }
  }

  if (is_maximal) {
    for (int i = 0, n = work.size(); i &lt; n; ++i)
      cout &lt;&lt; work[i] &lt;&lt; &quot; &quot;;
    cout &lt;&lt; '\n';
  }
}
</code></pre>
<p>Aufruf:</p>
<pre><code class="language-cpp">int valuesArray[] = {4, 3, 5};
const int nValues = sizeof(valuesArray) / sizeof(valuesArray[0]);

vector&lt;int&gt; values(valuesArray, valuesArray + nValues);
vector&lt;int&gt; work;

print_combinations_max(values, 0, work, 11);
</code></pre>
<p>Ausgabe:</p>
<pre><code>4 4 3 
4 3 4 
4 3 3 
4 5 
3 4 4 
3 4 3 
3 3 4 
3 3 3 
3 3 5 
3 5 3 
5 4 
5 3 3 
5 5
</code></pre>
<p>Zur Erkärung:<br />
Rekursion braucht immer einen Basisfall und eine Regel, wie ein komplizierter Fall auf einen einfacheren Fall reduziert werden kann, so dass irgendwann der Basisfall erreicht wird. In diesem Fall lässt sich die Aufgabe, alle Kombinationen aus einer Menge {a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>} bis zu einer Maximalsumme M zu finden folgendermaßen auf kleinere Aufgaben zurückführen: Für a<sub>1</sub> bis a<sub>n</sub> bildet man jeweils alle Kombinationen bis zu einer nicht-negativen Maximalsumme von M - a<sub>i</sub> und hängt a<sub>i</sub> davor. Der Basisfall ergibt sich automatisch dadurch, dass das nicht möglich ist, wenn M - a<sub>i</sub> irgendwann negativ wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1999578</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1999578</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Tue, 28 Dec 2010 10:39:02 GMT</pubDate></item></channel></rss>