数据结构实验之栈一:进制转换

数据结构实验之栈一:进制转换

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

输入一个十进制整数,将其转换成对应的R(2<=R<=9)进制数,并输出。

Input

第一行输入需要转换的十进制数;
第二行输入R。

Output

输出转换所得的R进制数。

Example Input

1279
8

Example Output

2377

#include<iostream>
#include <stack>
using namespace std;
int main()
{
    int N,m;
    cin>>N>>m;//N-要转化的数,要转化成m进制
    stack<int>S;
    if(N==0) //无论几进制,输入0,结果都是0
    {
        cout<<"0"<<endl;
    }
    if(N<0)//如果是负数,先变成正数
    {
        N=-N;
    }
    else
    {
        while(N)
        {
            S.push(N%m);//取余进栈
            N=N/m;
        }
        while(!S.empty()) //先进后出,直到栈空
        {
            cout<<S.top();;
            S.pop();
        }
        cout<<endl;
    }
    return 0;
}


posted @ 2016-09-28 21:29  Philtell  阅读(118)  评论(0编辑  收藏  举报