码迷,mamicode.com
首页 > 其他好文 > 详细

进制转换 2031

时间:2015-07-05 09:28:47      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description

输入一个十进制数N,将它转换成R进制数输出。
 

 

Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
 

 

Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
 

 

Sample Input
7 2 23 12 -4 3
 

 

Sample Output
111 1B -11
 

 

Author
lcy
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2029 2028 2044 2041 2040 
 
 
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 void ttor(int n, int r)
 5 {
 6     if (n)
 7     {
 8         ttor(n / r, r);
 9         printf("%c", n % r > 9 ? n % r - 10 + A : n % r + 0);
10     }
11 }
12 
13 int main(void)
14 {
15     int n;
16     int r;
17 
18     while (scanf("%d%d", &n, &r) != EOF)
19     {
20         if (n > 0)
21             ttor(n, r);
22         else if (!n)
23             putchar(0);
24         else
25         {
26             putchar(-);
27             ttor(-n, r);
28         }
29         putchar(\n);
30     }
31 
32     return 0;
33 }

 

进制转换 2031

标签:

原文地址:http://www.cnblogs.com/wangmengmeng/p/4621652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!