Algorithm Ideas:
(1) read the n-base number and convert it to 10-base first.
(2) then convert decimal to M.
(3) data structures, stacks, and queues to be used
Code As follows (n, m must be less than 10 .)
Queue. h # Ifndef _ queue _ H
# Define _ Queue _ H
# Define Maxcompute 100
Class Queue
{
Public :
Queue ()
{
Head=Tail= 0;
}
Void Enqueue ( Int Value)
{
If (Tail + 1 ) % Max = Head) Return ;
S [tail] = Value;
Tail = (Tail + 1 ) % Max;
}
Int Dequeue ()
{
If (Head = Tail) Return 0 ;
Int E;
E = S [head];
Head = (Head + 1 ) % Max;
Return E;
}
Bool Isempty ()
{
ReturnTail=Head;
}
Int Count ()
{
Return(Tail-Head+Max)%Max;
}
Private :
Int S [Max];
Int Head;
Int Tail;
} ;
# Endif
Stack. h # Ifndef _ stack _ H
# Define _ Stack _ H
# Define Maxcompute 100
Class Stack
{
Public :
Stack ()
{
Top= 0;
}
Bool Isempty ()
{
ReturnTop= 0;
}
Void Push ( Int Value)
{
If (Top <= Max)
{
S [Top]=Value;
Top++;
}
}
Int Pop ()
{
If (Top > = 0 )
{
Top--;
ReturnS [Top];
}
Else
Return - 1 ;
}
Private :
Int S [Max];
Int Top;
} ;
# Endif
Master Program : # Include " Queue. h "
# Include " Stack. h "
# Include < Stdio. h >
Int Main ()
{
Int N, m, X, Sum = 0 ;
Queue Q;
Stack S;
Scanf ( " % D " , & N, & M );
Scanf ( " % D " , & X );
While (X > = 10 )
{
Q. enqueue (x%10);
X=X/10;
}
Q. enqueue (x % 10 );
Int Count = Q. Count ();
For ( Int I = 0 ; I < Count; I ++ )
{
If (Q. isempty ())
Break ;
X = Q. dequeue ();
If (X = 0 )
Continue ;
For ( Int J = 0 ; J < I; j ++ )
X * = N;
Sum + = X;
}
While (Sum > 0 )
{
S. Push (Sum%M );
Sum=Sum/M;
}
While ( ! S. isempty ())
{
Printf ("% D", S. Pop ());
}
Return 0 ;
}