当前位置: 代码迷 >> 综合 >> 进制转换 oj
  详细解决方案

进制转换 oj

热度:5   发布时间:2024-01-14 22:47:18.0

进制转换

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R != 10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Example Input
7 2
23 12
-4 3
Example Output
111
1B
-11
Hint
Author
HDOJ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX 100001000
char a[18] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','-'};
int b[100];
int top;
void ans(int n,int m)
{
    int flag = 0;
    if(n < 0)
    {
        flag = 1;
        n = -n;
    }
    while(n)
    {
        b[top++] = n % m;
        n /= m;
    }
    if(flag)
    {
        b[top++] = 16;
    }
}
int main()
{
    int n,m,i;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        top = 0;
        ans(n,m);
        for(i = top-1;i >= 0;i--)
        {
            printf("%c",a[b[i]]);
        }
        if(n == 0)
            printf("0");
        printf("\n");
    }
    return 0;
}

  相关解决方案