进位转换(杭电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<stdio.h>
int main()
{
    int N,R;
    char a[35];
    char b[]="0123456789ABCDEF";
    while(~scanf("%d %d",&N,&R))
    {
        int t=0,i;
        if(N<0)
        {
            printf("-");
            N=N*(-1);
        }
       
        for(i=0;;i++)//重复求余,最后倒着将数组数据输出
        {
            a[i]=b[N%R];
            t++;
            N=N/R;
            if(N==0)
            break;
        }
       
        while(t--)
        {
            printf("%c",a[t]);
        }
        printf(" ");
    }
    while(1);
    return 0;
   
}
           

原文地址:https://www.cnblogs.com/lcchuguo/p/4047618.html