H-進数変換

3080 ワード

H-進変換Time Limit:1000 MS Memory Limit:32768 KB 64 bit IO Format:%I 64 d&%I 64 u Submit
Status
Practice
HDU 2031 Descriptionは、10進数Nを入力し、R進数出力に変換する.
Input入力データは複数の試験例を含み、各試験例は2つの整数N(32ビット整数)とR(2<=R<=16,R<>10)を含む.
Outputは、テストインスタンスごとに変換された数を出力し、出力ごとに1行を占めます.Rが10より大きい場合、対応する数値規則は16進数(例えば、10はAで表されるなど)を参照する.
Sample Input 7 2 23 12 -4 3
Sample Output 111 1B -11
スタックの先進的な後出性を利用して、プロセススタックの基本的な操作を簡素化することができます.http://blog.csdn.net/qq_32680617/article/details/50634920
処理時の注意入力は負の数であり、10より大きい進数を要求する場合は、16進数を真似て大文字を出力します.
コード#コード#
#include<cstdio>
#include<stack>
#include<cstring>
#include<cctype>
#include<cstdlib>
using namespace std;
int main()
{
    int n,r;
    while(scanf("%d%d",&n,&r)!=EOF)
    {
        bool flag=0;//n 1;
        if(n<0)
        {
            n=-n;
            flag=1;
        }
        stack<int>opnd;
        while(n>=r)
        {
            opnd.push(n%r);
            n=n/r;
        }
        if(n);
        opnd.push(n);
        if(flag)
            printf("-");
        while(1)
        {
            if(opnd.empty())
            {
                printf("
"
); break; } int x=opnd.top(); if(x>=10) { printf("%c",x%10+'A'); opnd.pop(); } else { printf("%d",x); opnd.pop(); } } } }