
求十进制100转换成N进制的C语言编程,谢谢!
推荐于2018-04-27 · 知道合伙人教育行家
关注

展开全部
参考代码:
#include <stdio.h>
char *fn(int n, int to)
{
//定义静态变量的目的:为了在多层递归里面使用的是同一个值,且static只执行一次
static char buf[100] = "", BinHex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//进行n == 0 和 n < 0 的情况处理,static只执行一次
static int i = (n >= 0) ? (buf[0] = '0',0) : (buf[0] = '-',n = -n,1);
//正整数递归,进行进制转换.
if (n != 0)
{
//递归入栈
fn(n/to,to);
//递归弹栈
buf[i++] = BinHex[n%to];
}
return buf;
}
int main()
{
char *buf = NULL;
int n=100, to;
//输入n:原数据 to:需要转化成的进制
scanf("%d",&to);
//进制转换
buf = fn(n,to);
puts(buf);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询