思路:
使用B题中实现的通用转换算法即可。
解答:
#include <cstdio>
#include <cstring>
using namespace std;
void oct_to_char(int temp, int b, char output[]) { //将10进制数temp转换成字符表示的b进制数,存储在字符串output中
int i = 0;
do {
int k = temp % b;
if (k <= 9)
output[i++] = '0' + k; // int转char的方法
else
output[i++] = 'A' + (k - 10);
temp /= b;
} while (temp != 0);
output[i] = '\0'; // 必须添加结束符, 不然strlen无法正确判别长度
}
int main() {
int input; //待转换的数
while (scanf("%d", &input) != EOF) {
char output[100]; // 存放结果的字符数组
oct_to_char(input, 8, output); // 十进制转换为目标进制b
for(int j = strlen(output) - 1; j >= 0; j--)
printf("%c", output[j]);
printf("\n");
}
return 0;
}