题目描述
给定一个整数 num,将其转化为 7 进制,并以字符串形式输出。
问题分析
按照二进制转换的方式进行转换即可
代码
char* convertToBase7(int num) {
int count = 0;
char *x = (char *)malloc(sizeof(char)*32);
char *y = (char *)malloc(sizeof(char)*32);
if(num>0){
while(num!=0){
x[count] = num%7+'0';
num/=7;
count=count+1;
}
x[count] = 0;
for(int i=0; i<count; i++){
y[i] = x[count-1-i];
}
y[count] = 0;
}else if(num==0){
x[0] = '0';
x[1] = 0;
return x;
}else{
num = -1*num;
while(num!=0){
x[count] = num%7+'0';
num/=7;
count++;
}
for(int i=0; i<count; i++){
y[i+1] = x[count-1-i];
}
y[0] = '-';
y[count+1] = 0;
}
return y;
}