|
发表于 2021-10-24 20:07:25
|
显示全部楼层
- #include <stdio.h>
- char * foo(char s[] , int x , int n)
- {
- const char b[] = {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F'} ;
- int d , e , i ;
- s[0] = '\0' ;
- if(n >= 1 && n <= 16) {
- for(d = x , e = 1 ; x / e > n - 1 ; e *= n) ;
- for(i = 0 ; e ; d %= e , e /= n , i ++) s[i] = b[d / e] ;
- s[i] = '\0' ;
- }
- return s ;
- }
- int main(void)
- {
- char s[256] ;
- int d , n ;
- scanf("%d%d" , & d , & n) ;
- printf("%s\n" , foo(s , d , n)) ;
- }
复制代码
编译、运行实况:
- D:\00.Excise\C>g++ -o x x.c
- D:\00.Excise\C>x
- 255 16
- FF
- D:\00.Excise\C>x
- 255 2
- 11111111
- D:\00.Excise\C>x
- 255 8
- 377
- D:\00.Excise\C>x
- 256 16
- 100
- D:\00.Excise\C>x
- 256 2
- 100000000
- D:\00.Excise\C>x
- 256 8
- 400
- D:\00.Excise\C>
复制代码 |
|