进制转换
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19010 Accepted Submission(s): 10693
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
CODE:
#include<stdio.h>
int a[100005]; //用来记录余数,便于倒序输出
int main()
{
int n,r,s;
while (scanf("%d%d",&n,&r)!=EOF)
{
int i=0,j;
if(n==0)
printf("0\n");
if(n<0) //判断数的正负
{
printf("-");
n=-n;
}
{
while(n)
{ s=n%r;
n=n/r;
i++;
if(s<10)
{
a[i]=s+'0';
}
if(s>=10)
{
s=s-10+'A';
a[i]=s;
}
}
for(j=i;j>=1;j--)
printf("%c",a[j]);
printf("\n");
}
}
return 0;
}
进制转换:
十进制8转为二进制, 8/2=4...0 4/2=2...0 2/2=1...0 1/2=0....1 (一直除到商为0为止)
将余数倒序写出:
8=(1000)2