Pagini recente » Cod sursa (job #856259) | Cod sursa (job #1452860) | Cod sursa (job #1187507) | Cod sursa (job #1110148) | Cod sursa (job #547117)
Cod sursa(job #547117)
#include<cstdio>
using namespace std;
struct NodLS
{
int inf,pas;
struct NodLS *next,*prec;
};
typedef NodLS* NLS;
NLS a[10];
NLS sf[10];
int maxx,n,nr,cif=10;
void read();
void solve();
void write();
int main()
{
freopen ("algsort.in","r",stdin);
freopen ("algsort.out","w",stdout);
read();
while (maxx)
{
++nr;
maxx/=10;
}
solve();
write();
return 0;
}
void write()
{
int i;
NLS p;
for (i=0;i<10;++i)
for (p=a[i];p;p=p->next)
printf("%d ",p->inf);
}
void read()
{
int i,x,r;
scanf ("%d",&n);
for (i=1;i<=n;++i)
{
scanf("%d ",&x);
maxx=(x>maxx?x:maxx);
NLS p=new NodLS;
p->inf=x;
p->pas=0;
r=x%10;
p->prec=sf[r];
p->next=NULL;
if (!sf[r])
sf[r]=p;
else
{
sf[r]->next=p;
sf[r]=p;
}
if (!a[r])
a[r]=sf[r];
}
}
void solve()
{
int i,q,r;
NLS p,z;
for (q=1;q<nr;++q)
{
for (i=0;i<10;++i)
for (p=a[i];p;p=z)
{
z=p->next;
r=p->inf/cif%10;
if (q!=p->pas)
{
p->pas=q;
if (p->next&&p->prec)
{
p->prec->next=p->next;
p->next->prec=p->prec;
}
else if (p->next)
{
a[i]=p->next;
a[i]->prec=NULL;
}
else if (p->prec)
{
sf[i]=p->prec;
sf[i]->next=NULL;
}
else
a[i]=sf[i]=NULL;
p->prec=sf[r];
p->next=NULL;
if (!sf[r])
sf[r]=p;
else
{
sf[r]->next=p;
sf[r]=p;
}
if (!a[r])
a[r]=sf[r];
}
}
cif*=10;
}
}