进制转换2 分类: 栈和队列 2015-06-09 10:25 14人阅读 评论(0) 收藏

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

TimeLimit: 1000ms Memory limit: 65536K 有疑问?点这里^_^

题目描述

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

输入

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

第二行输入R

输出

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

示例输入

1279

8

示例输出

2377

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <vector>
#include <string>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout)
#define INF 0x3f3f3f3f

using namespace std;

int main()
{
    int n,m;
    cin>>n>>m;

    if(n==0)
    {
        cout<<0<<endl;

    }
    else
    {
        if(n<0)
        {
            cout<<'-';
            n=-n;
        }
        stack<int>B;
        while(n)
        {
            B.push(n%m);
            n/=m;
        }
        while(!B.empty())
        {
            cout<<B.top();
            B.pop();
        }
        cout<<endl;
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/juechen/p/4722052.html