题解 | #十六进制转十进制#
十六进制转十进制
http://www.nowcoder.com/practice/33e148570d5c4e728116e2f861638c9c
这道题一共有两种思路: 思路一: 直接将十六进制数字赋值到a
int main()
{
int a = 0xABCDEF;
printf("%15d", a);
}
思路二:比较简单,利于理解,但步骤比较复杂,这种方法的难点在于输出时pow的调用中i与实际间的关系!!!
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(void)
{
char arr[] = "ABCDEF";
int i = 0, j = 0;
double sum = 0.0;
char ch = arr[0];
int num[10] = { 0 };
int n = strlen(arr);
int m = 0;
while (ch != '\0')
{
ch = arr[i];
switch (ch)
{
case 'A':num[j] = 10;
break;
case 'B':num[j] = 11;
break;
case 'C':num[j] = 12;
break;
case 'D':num[j] = 13;
break;
case 'E':num[j] = 14;
break;
case 'F':num[j] = 15;
break;
default: break;
}
if (num[i] != 0)
{
i++;
j++;
}
}
for (i = n-1; i >=0; i--)
sum += num[i] * pow(16,n-1-i);
m = (int)sum;
printf("%15d", m);
return 0;
}