进制转换
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41581 Accepted Submission(s): 22802
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
C语言程序设计练习(五)
Recommend
lcy | We have carefully selected several similar problems for you:
2044
1020
2048
2090
2046
Statistic |
Submit |
Discuss |
Note
可以模拟进制转换的过程,然后倒序输出。
<span style="font-family: Arial, Helvetica, sans-serif;">#include<stdio.h></span>
int c[1000000];
int main() {
int n,r;
while(scanf("%d %d",&n,&r)==2) {
if(n<0) {
printf("-");
n=-n;
}
if(n==0)
printf("0");
int i=0;
while(n!=0) {
c[i]=n%r;
n=n/r;
i++;
}
for(int l=i-1; l>=0; l--) {
if(c[l]==10)
printf("A");
else if(c[l]==11)
printf("B");
else if(c[l]==12)
printf("C");
else if(c[l]==13)
printf("D");
else if(c[l]==14)
printf("E");
else if(c[l]==15)
printf("F");
else
printf("%d",c[l]);
}
printf("\n");
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-1299850.html