Hexadecimal conversion
Subject description:
Enter a decimal number N, it converts into R decimal output.
Input
Input data comprising a plurality of test cases, each test case contains two integers N (32-bit integer) and R (2 <= R <= 16, R <> 10).
Output
After each test the number of examples of output converter, each output per line. If rule number R is greater than 10, then the corresponding hexadecimal reference (for example, represented by 10 A, etc.).
Sample Input
7 2 23 12 -4 3Sample Output
111 1B -11
By the answer:
#include <stdio.h>
#include<math.h>
int main() {
int n,r;
int z[10000];
while (scanf("%d %d", &n,&r)!=EOF) {
int i=0,num,z[10000]; //每输入两个数就要进行初始化
for(num=abs(n);num!=0;i++) //取绝对值
{ //将十进制数y转换为Q进制数z:采用“除基取余法”
z[i]=num%r;
num=num/r;
}
i--; //将余数从后往前输出即为所求数的二进制数
if (n < 0) //当输入的数是负数时,需要输出负号
printf("-");
for(; i >= 0; i--){ //如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)
if(z[i]<10)
printf("%d",z[i]);
else if(z[i]==10)
printf("A");
else if(z[i]==11)
printf("B");
else if(z[i]==12)
printf("C");
else if(z[i]==13)
printf("D");
else if(z[i]==14)
printf("E");
else if(z[i]==15)
printf("F");
}
printf("\n"); //换行
}
return 0;
}