YTU 1942 C语言习题5.19--进制转换

版权声明:转载请附上原文链接哟! https://blog.csdn.net/weixin_44170305/article/details/90173326

不恋尘世浮华,不写红尘纷扰,不叹世道苍凉,不惹情思哀怨,闲看花开,静待花落,冷暖自知,干净如始。

题目描述

输入一个十进制数N,将它转换成R进制数输出。

输入

输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。

输出

为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。

样例输入

copy

7 2
23 12
-4 3

样例输出

111
1B
-11
#include<stdio.h>
void hanshu(int n, int r)
{
    if(n>=r)
        hanshu(n/r,r);
    int t=n%r;
    if(t<10)
        putchar(t+'0');
    else
        putchar(t-10+'A');
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)==2)
    {
        if(n==0)
        {
            printf("0\n");
            continue;
        }
        if(n<0)
        {
            printf("-");
            n=-n;
        }
        hanshu(n,m);
        printf("\n");
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/weixin_44170305/article/details/90173326