题目:写一个函数,输入一个十进制的数,将其转换为任意的r进制数
#include<stdio.h>
void convertToBaseR(int num, int r);
int main() {
int num, r;
printf("请输入十进制的整数:");
scanf_s("%d", &num);
printf("请输入要转化的进制数:");
scanf_s("%d", &r);
convertToBaseR(num, r);
return 0;
}
void convertToBaseR(int num, int r) {
int index = 0;
char result[65];
if (num == 0) {
printf("0\n");
return;
}
while (num > 0) {
int remainder = num % r;
if (remainder < 10) {
result[index++] = '0' + remainder;
}
else
{
result[index++] = 'A' + (remainder - 10);
}
num /= r;
}
result[index] = '\0';
printf("转换后的值为:");
for (int i = index - 1; i >= 0; i--)
{
printf("%c", result[i]);
}
printf("\n");
}