- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 将十六进制数转换成十进制数
- 输入
- 第一行有一个整数T,表示共有T组数据
接下来T行,每一行为一个16进制无符号正整数,位数不超过8位,数中的a-f均为大写字母,数前没有多余的0
- 输出
- 输出共T行,每一行是一组数据的十进制表示,数字前不得有多余的0。
十进制数小于2^31
- 样例输入
4 A F FFFE 10001
- 样例输出
10 15 65534 65537
-
用个map就好~ 还是要注意pow()函数~
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "map"
#include "cmath"
using namespace std;
typedef long long ll;
const int maxn = 10;
map<char, ll> m;
int main(int argc, char const *argv[])
{
int t;
scanf("%d", &t);
m['0'] = 0, m['1'] = 1, m['2'] = 2, m['3'] = 3, m['4'] = 4, m['5'] = 5, m['6'] = 6, m['7'] = 7, m['8'] = 8, m['9'] = 9, m['A'] = 10, m['B'] = 11, m['C'] = 12, m['D'] = 13, m['E'] = 14, m['F'] = 15;
while(t--) {
char s[maxn];
scanf("%s", s);
ll ans = 0;
int len = strlen(s), num = 0;
for(int i = len - 1; i >= 0; --i)
ans += ((ll)pow(16, num++) * m[s[i]]);
printf("%lld\n", ans);
}
return 0;
}