Convert n to m

Source: Internet
Author: User
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 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.