码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2031 进制转换

时间:2018-07-17 00:44:30      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:ott   ble   desc   targe   bottom   lse   NPU   const   --   

http://acm.hdu.edu.cn/showproblem.php?pid=2031

 

Problem Description
输入一个十进制数N,将它转换成R进制数输出。
 
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
 
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
 
Sample Input
7 2
23 12
-4 3
 
Sample Output
111
1B
-11
 
代码:
#include <bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;
char num[20]= {‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘};
char ans[maxn];

int main()
{
    int n,d;
    while(~scanf("%d%d",&n,&d))
    {
        int numm=0;
        if(n<0)
        {
            n=n*-1;
            printf("-");
        }
        do
        {
            ans[numm++]=num[n%d];
            n/=d;
        }
        while(n!=0);
            for(int i=numm-1;i>=0;i--)
            {
                if(i!=0)
                    printf("%c",ans[i]);
                else
                    printf("%c\n",ans[i]);
            }
    }
    return 0;
}

  

HDU 2031 进制转换

标签:ott   ble   desc   targe   bottom   lse   NPU   const   --   

原文地址:https://www.cnblogs.com/zlrrrr/p/9321165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!