Cod sursa(job #1004383)

Utilizator andreey_047Andrei Maxim andreey_047 Data 2 octombrie 2013 17:44:43
Problema Generare de permutari Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <fstream>
#include <algorithm>

using namespace std;
    int a[101],t[101],c[101],jj;
int main()
{   int n,i,nr,aux,j,ok,k,kk;
  ifstream fin("permutari.in");
  ofstream fout("permutari.out");
    fin >> n;
    fin.close();
        for(i = 1;i<=n;i++) {a[i] = i; c[i] = i;}
    for(i = 1;i<=n;i++) fout << a[i]<<" ";

        fout <<"\n";
        ok = 0; i = n;
    while(ok == 0)
    {

        if (a[i] > a[i-1])
        {
              for(j = 1;j<=n;j++)
                    t[j] = a[j];
            aux = a[i];
            a[i] = a[i-1];
            a[i-1] = aux;


        }
           else if (a[i] < a[i-1])
            {
                    for(j = 1;j<=n;j++)
                    t[j] = a[j];

                aux = a[i];
                a[i] = a[i-2];
                a[i-2] = aux;
                    sort(a  + i-1 ,a+n+1);
            }

            k = 0;
                     for(j = 1;j<=n && k == 0 ;j++)
                       if (a[j] != c[j]) k = 1;


        if (k == 0)
            {
            i--;
                for(j = 1;j<=n;j++)
                    a[j] = t[j];
            }

        else
        {       kk = 0;
                aux = 1;
               for(j = 1;j<=n;j++) {fout << a[j]<<" "; if(a[j] != c[n-kk]) aux = 0; kk++;}
                    fout <<"\n";
                        if (aux == 1) ok = 1;

        }


    }

    fout.close();
    return 0;
}