Basic practice hex-to-decimal time limit: 1.0s memory Limit: 512.0MB problem description Enter a positive hexadecimal number string that does not exceed 8 bits from the keyboard, converting it to a positive decimal number after the output.
Note: 10~15 in hexadecimal numbers are denoted by uppercase letters A, B, C, D, E, F, respectively. Sample input FFFF sample output 65535 because the problem requires an input string, it cannot be entered as%x directly, and the output is%d, which is not 100% correct.
#include <stdio.h>
#include <string.h>
#include <math.h>
int main ()
{
char a[ ];
scanf ("%s", a);
int L=strlen (a);
A long long ans=0;//note is a positive hexadecimal number string of no more than 8 bits, so the number is larger and should be set to long long for
(int i=0; i<l; i++)
{
if (a[i]>= ' 0 ') &&a[i]<= ' 9 ')
ans+= (a[i]-' 0 ') *pow (16*1.0,l-1-i);
else
ans+= (a[i]-' a ' +10) *pow (16*1.0,l-1-i);
}
printf ("%lld\n", ans);
return 0;
}