Cod sursa(job #1339813)

Utilizator sebastian123aldea sebastian sebastian123 Data 11 februarie 2015 10:29:51
Problema Generare de permutari Scor 20
Compilator c Status done
Runda Arhiva educationala Marime 0.98 kb
#include<stdio.h>
#define SWAP(a,b) aux=a;a=b;b=aux
void afisare(int st[],int n,FILE *qFile)
{
    int i;
    for(i=0;i<n;i++)
        fprintf(qFile,"%i ",st[i]);
    fprintf(qFile,"\n");
}

void f(int st[],int u[],int n,int k,FILE *qFile)
{
    int i,j,aux;
    i=n-1;
    while(i>0&&st[i-1]>=st[i])
    {
        i--;
    }
    if(i==0)
    {
        return;
    }
    j=n-1;
    while(st[j]<=st[i-1])
    {
        j--;
    }
    SWAP(st[i-1],st[j]);
    j=n-1;
    while(i<j)
    {
        SWAP(st[i],st[j]);
        i++;
        j--;
    }
    afisare(st,n,qFile);
    f(st,u,n,0,qFile);
}

int main()
{
    FILE *pFile,*qFile;
    pFile=fopen("permutari.in","r");
    qFile=fopen("permutari.out","w");

    int i,st[100],u[100],n;
    //fscanf(pFile,"%i",&n);
    n=4;
    for(i=0;i<=n;i++)
    {
        st[i]=i+1;
        u[i]=0;
    }
    afisare(st,n,qFile);
    f(st,u,n,0,qFile);
    fclose(pFile);
    fclose(qFile);
    return 0;
}