Cod sursa(job #1436699)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 16 mai 2015 12:47:49
Problema Order Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <cstdlib>
using namespace std;
ifstream f("order.in");
ofstream g("order.out");
int n, i, e;

struct nod{
    int v;
    nod *st;
    nod *dr;
};
typedef nod *NOD;
NOD p, q, u;

int main(){
    f>>n;
    p=(NOD)malloc(sizeof(nod));
    p->st=NULL;
    p->dr=NULL;
    p->v=1;
    u=p;
    for(i=2; i<=n; i++)
    {
        q=(NOD)malloc(sizeof(nod));
        u->dr=q;
        q->st=u;
        q->dr=NULL;
        q->v=i;
        u=q;
    }
    p->st=u;
    u->dr=p;
    i=1;
    q=p;
    while(n>0)
    {
        e=i%n;
        if(n==1)
            e=1;
        while(e>0)
        {
            q=q->dr;
            e--;
        }
        g<< q->v <<' ';
        q->st->dr=q->dr;
        q->dr->st=q->st;
        i++;
        n--;
    }
    return 0;
}