<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0205_-_Shuffle</id>
	<title>0205 - Shuffle - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0205_-_Shuffle"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0205_-_Shuffle&amp;action=history"/>
	<updated>2026-05-02T10:15:53Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.universitas.ro/index.php?title=0205_-_Shuffle&amp;diff=9155&amp;oldid=prev</id>
		<title>Rus Marius: Pagină nouă: = Cerinţa = Se citeşte un număr natural nenul &lt;code&gt;n&lt;/code&gt; şi o permutare a mulţimii &lt;code&gt;M={1,2,..,n}&lt;/code&gt;. Să se afişeze, în ordine lexicografică, toate permutările mulţimii M care nu conţin elemente alăturate care au fost alăturate şi în permutarea dată.  = Date de intrare = Fişierul de intrare &lt;code&gt;shuffleIN.txt&lt;/code&gt; conţine pe prima linie numărul &lt;code&gt;n&lt;/code&gt;, iar pe a doua linie &lt;code&gt;n&lt;/code&gt; valori distincte cuprinse între &lt;code&gt;1&lt;/code&gt;...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0205_-_Shuffle&amp;diff=9155&amp;oldid=prev"/>
		<updated>2024-01-07T10:21:07Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerinţa = Se citeşte un număr natural nenul &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; şi o permutare a mulţimii &amp;lt;code&amp;gt;M={1,2,..,n}&amp;lt;/code&amp;gt;. Să se afişeze, în ordine lexicografică, toate permutările mulţimii M care nu conţin elemente alăturate care au fost alăturate şi în permutarea dată.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;shuffleIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie numărul &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, iar pe a doua linie &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; valori distincte cuprinse între &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Cerinţa =&lt;br /&gt;
Se citeşte un număr natural nenul &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; şi o permutare a mulţimii &amp;lt;code&amp;gt;M={1,2,..,n}&amp;lt;/code&amp;gt;. Să se afişeze, în ordine lexicografică, toate permutările mulţimii M care nu conţin elemente alăturate care au fost alăturate şi în permutarea dată.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fişierul de intrare &amp;lt;code&amp;gt;shuffleIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie numărul &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, iar pe a doua linie &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; valori distincte cuprinse între &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, separate prin câte un spațiu, reprezentând permutarea dată.&lt;br /&gt;
&lt;br /&gt;
= Date de ieşire =&lt;br /&gt;
Fişierul de ieşire &amp;lt;code&amp;gt;shuffleOUT.txt&amp;lt;/code&amp;gt; va conţine pe fiecare linie elementele unei permutări, separate prin câte un spaţiu.&lt;br /&gt;
&lt;br /&gt;
= Restricţii şi precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;0 &amp;lt; n &amp;lt; 9&amp;lt;/code&amp;gt;&lt;br /&gt;
* dacă nu există soluţii se va afişa pe prima linie a fişierului de ieşire mesajul &amp;lt;code&amp;gt;nu exista&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;shuffleIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 4&lt;br /&gt;
 2 3 1 4&lt;br /&gt;
&amp;lt;code&amp;gt;shuffleOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 1 2 4 3 &lt;br /&gt;
 3 4 2 1 &lt;br /&gt;
&lt;br /&gt;
= Exemplul 2: =&lt;br /&gt;
&amp;lt;code&amp;gt;shuffleIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 11&lt;br /&gt;
 2 3 1 4&lt;br /&gt;
&amp;lt;code&amp;gt;shuffleOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 Datele nu corespund restrictiilor impuse&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python3&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
def citire():&lt;br /&gt;
    with open(&amp;quot;shuffleIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as input_file:&lt;br /&gt;
        n = int(input_file.readline().strip())&lt;br /&gt;
        v = [0] + list(map(int, input_file.readline().split()))&lt;br /&gt;
    return n, v&lt;br /&gt;
&lt;br /&gt;
def prim(num):&lt;br /&gt;
    if num &amp;lt; 2:&lt;br /&gt;
        return False&lt;br /&gt;
    if num % 2 == 0 and num != 2:&lt;br /&gt;
        return False&lt;br /&gt;
    for i in range(3, int(num**0.5) + 1, 2):&lt;br /&gt;
        if num % i == 0:&lt;br /&gt;
            return False&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def sortare(n, v):&lt;br /&gt;
    for i in range(1, n):&lt;br /&gt;
        for j in range(i + 1, n + 1):&lt;br /&gt;
            if not prim(v[i]) and not prim(v[j]) and v[i] &amp;gt; v[j]:&lt;br /&gt;
                v[i], v[j] = v[j], v[i]&lt;br /&gt;
&lt;br /&gt;
def puncte_fixe(n, v):&lt;br /&gt;
    x = [0] * (n + 1)&lt;br /&gt;
    for i in range(1, n + 1):&lt;br /&gt;
        if prim(v[i]):&lt;br /&gt;
            x[i] = i&lt;br /&gt;
    return x&lt;br /&gt;
&lt;br /&gt;
def afisare(n, v, x, output_file):&lt;br /&gt;
    result = [v[x[i]] for i in range(1, n + 1) if x[i] != 0]&lt;br /&gt;
    output_file.write(&amp;quot; &amp;quot;.join(map(str, result)) + &amp;quot;\n&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
def valid(k, x):&lt;br /&gt;
    for i in range(1, k):&lt;br /&gt;
        if x[i] == x[k]:&lt;br /&gt;
            return False&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def backtracking(k, n, v, x, output_file):&lt;br /&gt;
    if k &amp;gt; n:&lt;br /&gt;
        afisare(n, v, x, output_file)&lt;br /&gt;
    else:&lt;br /&gt;
        if prim(v[k]):&lt;br /&gt;
            backtracking(k + 1, n, v, x, output_file)&lt;br /&gt;
        else:&lt;br /&gt;
            for i in range(1, n + 1):&lt;br /&gt;
                if not prim(v[i]):&lt;br /&gt;
                    x[k] = i&lt;br /&gt;
                    if valid(k, x):&lt;br /&gt;
                        backtracking(k + 1, n, v, x, output_file)&lt;br /&gt;
&lt;br /&gt;
def verifica_restrictii(n):&lt;br /&gt;
    if 0 &amp;lt; n &amp;lt; 9:&lt;br /&gt;
        return True&lt;br /&gt;
    else:&lt;br /&gt;
        with open(&amp;quot;shuffleOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as output_file:&lt;br /&gt;
            output_file.write(&amp;quot;Datele nu corespund restrictiilor impuse\n&amp;quot;)&lt;br /&gt;
        return False&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    n, v = citire()&lt;br /&gt;
&lt;br /&gt;
    if verifica_restrictii(n):&lt;br /&gt;
        sortare(n, v)&lt;br /&gt;
        x = puncte_fixe(n, v)&lt;br /&gt;
&lt;br /&gt;
        with open(&amp;quot;shuffleOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as output_file:&lt;br /&gt;
            backtracking(1, n, v, x, output_file)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rus Marius</name></author>
	</entry>
</feed>