#include <stdio.h> #define MAX_ARRAY 0x00001000 main() { int k=0,n=0,i=0; char a[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 匹配字符串 char transfered[MAX_ARRAY] = {0}; scanf("%d %d",&k,&n); for (i=0; n; ++i) { transfered[i] = a[n % k]; n /= k; } for (--i; i>=0 ; --i) { putchar(transfered[i]); } putchar('/n'); } 使用栈的做法 #include "stdio.h" #define M 100 typedef struct { int stack[M]; int top; } StackType ; void _putchar( int, int ); void Conversion( long x, int R ) { int e; StackType st; /* 开设栈空间并初始化 */ st.top = 0; do /* 不断求得x除以R的余数并压栈 */ { st.stack[++st.top] = x % R; /* PUSH */ x = x / R ; } while( x != 0 ); while( st.top > 0 ) /* 不断退栈并将退栈元素按R进制符号输出 */ { e = st.stack[st.top--]; /* POP */ _putchar( e, R ); /* e按R进制符号输出 */ } } void _putchar( int d, int R ) /* 字符转换输出例:R=16 */ { char *r="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if(R>36) putchar('*'); else putchar( *(r+d) ); } void main() { long x; scanf("%ld",&x); Conversion(x,16); }