答案:
#include<stdio.h>
char fun(int n)
{if (n >= 0 && n < 10) //如果是小于10进制的return n + 48; //ASCII值+48else if (n >= 10 && n <= 16) //如果是大于10进制小于16进制的return n + 55; //ASCII值+55elsereturn 0;
}
int main()
{int N=0, R=0,t=0,z=0;while (scanf("%d %d", &N, &R) != EOF) {int a[100] = { 0 };int temp = 0;if (N > 0) //如果是正数{temp = N;}else if (N < 0) //如果是负数{temp = -N; //取反变正数}else //如果是0{printf("0\n"); //所有进制的0都是0continue; //跳回开始}int i = 0;for (; temp != 0; i++) //如果输入的不是0{a[i] = temp % R; //除R取余法temp /= R;}char m[100] = { '\0' };for (t = i - 1, z = 0; t >= 0; t--, z++) //将余数逆序{m[z] = fun(a[t]);}if (N < 0) //负数加个符号{printf("-");}puts(m); //打印N对应的R进制数}return 0;
}