Single Point Test time: 1.0 seconds
Memory Limit: 512 MB
Binary data is a number with 0 and 1, two digital represented. Its base 2, carry rule is "every two into a" borrow rule is "borrow one when two", a master of philosophy in Germany mathematical 18th century Levin Bu Nizi found.
hexadecimal (hex or subscript abbreviated as 16) in a mathematics every 16 into a binary system of general use numbers 0 to 9 and the letters a to F (or a ~ f) represents wherein: a ~ f represents 10 to 15, the hexadecimal numbers called.
Please binary number given to hexadecimal numbers, letters in lower case.
Entry
The first line of a positive integer T, T representatives of groups of test data (1≤T≤10)..
Next T rows each contain only input string 0
and 1
a string (no leading 0), the string length: 1 ≤length≤106.
Export
For each test case, the hexadecimal number after conversion and output on a separate line.
Sample
input
2 1 10
output
1 2
按四位二进制合成一位十六进制来考虑
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
map<int,char> mmp;
char s[1000005];
int a[1000000];
int main()
{
int t;
scanf("%d",&t);
mmp[10] = 'a';
mmp[11] = 'b';
mmp[12] = 'c';
mmp[13] = 'd';
mmp[14] = 'e';
mmp[15] = 'f';
while (t--)
{
int x = 0;
scanf("%s",s+1);
int len = strlen(s+1);
int k = 0,m = len / 4;
int sum = 0;
if (len<=4)//当输入不大于四位是可以直接输出
{
for (int i=len;i>=1;i--)
{
int p = s[i] - '0';
for (int j=0;j<k;j++)//k用来表示每一位x2的个数(倒着计算)
p *= 2;
sum += p;;
k++;
if (i==1)
{
a[x++] = sum;
break;
}
}
if (sum<=9)
printf("%d\n",sum);
else
printf("%c\n",mmp[sum]);
continue;
}
k = 0,sum = 0;
for (int i=len;i>len-m*4;i--)
{
int p = s[i] - '0';
for (int j=0;j<k;j++)
p *= 2;
sum += p;;
k++;
if (k==4)
{
k = 0;
a[x++] = sum;
sum = 0;
}
}
k = 0,sum = 0;
for (int i=len-m*4;i>=1;i--)
{
int p = s[i] - '0';
for (int j=0;j<k;j++)
p *= 2;
sum += p;;
k++;
if (i==1)
{
a[x++] = sum;
sum = 0;
break;
}
}
for (int i=x-1;i>=0;i--)
{
if (a[i]<=9)
printf("%d",a[i]);
else
printf("%c",mmp[a[i]]);
}
puts("");
}
return 0;
}