总结:
1.使用库函数可有效节省空间,但时间花费较多;
2.由于本题的输入数据较大,又限制时间,故要注意利用空间换时间;
3.使用顺序结构换取最小运行时间;
示例代码:
#include <stdio.h>
#include <string.h>
#define N1 100000 /*16进制*/
#define N2 400008 /*2进制*/
int main(void)
{
int n = 0 ;
int i = 0 , j = 0 , k = 0 , len = 0;
int flag = 0 , sum = 0 ;
char arr[N1] , num[N2];
scanf("%d",&n);
for (i = 0 ; i < n ; i ++)
{
memset(num,'\0',N2);
scanf("%s",&arr);
len = strlen(arr);
k = 0;
/*
16进制转2进制,再转8进制,不足3的倍数补0
*/
if (len%3 == 1)
{
num[0] = num[1] = '0' ;
k += 2;
}
else if (len%3 == 2)
{
num[0] = '0';
k += 1;
}
for (j = 0 ; j < len ; j ++ , k += 4)
{
switch(arr[j])
{
case '0' : num[k]='0',num[k+1]='0',num[k+2]='0',num[k+3]='0';break;
case '1' : num[k]='0',num[k+1]='0',num[k+2]='0',num[k+3]='1';break;
case '2' : num[k]='0',num[k+1]='0',num[k+2]='1',num[k+3]='0';break;
case '3' : num[k]='0',num[k+1]='0',num[k+2]='1',num[k+3]='1';break;
case '4' : num[k]='0',num[k+1]='1',num[k+2]='0',num[k+3]='0';break;
case '5' : num[k]='0',num[k+1]='1',num[k+2]='0',num[k+3]='1';break;
case '6' : num[k]='0',num[k+1]='1',num[k+2]='1',num[k+3]='0';break;
case '7' : num[k]='0',num[k+1]='1',num[k+2]='1',num[k+3]='1';break;
case '8' : num[k]='1',num[k+1]='0',num[k+2]='0',num[k+3]='0';break;
case '9' : num[k]='1',num[k+1]='0',num[k+2]='0',num[k+3]='1';break;
case 'A' : num[k]='1',num[k+1]='0',num[k+2]='1',num[k+3]='0';break;
case 'B' : num[k]='1',num[k+1]='0',num[k+2]='1',num[k+3]='1';break;
case 'C' : num[k]='1',num[k+1]='1',num[k+2]='0',num[k+3]='0';break;
case 'D' : num[k]='1',num[k+1]='1',num[k+2]='0',num[k+3]='1';break;
case 'E' : num[k]='1',num[k+1]='1',num[k+2]='1',num[k+3]='0';break;
case 'F' : num[k]='1',num[k+1]='1',num[k+2]='1',num[k+3]='1';break;
default:break;
}
}
for (j = 0 , flag = 0; j < k ; j += 3)
{
if (num[j+0] == '0' && num[j+1] == '0' && num[j+2] == '0')
sum = 0;
else if (num[j+0] == '0' && num[j+1] == '0' && num[j+2] == '1')
sum = 1;
else if (num[j+0] == '0' && num[j+1] == '1' && num[j+2] == '0')
sum = 2;
else if (num[j+0] == '0' && num[j+1] == '1' && num[j+2] == '1')
sum = 3;
else if (num[j+0] == '1' && num[j+1] == '0' && num[j+2] == '0')
sum = 4;
else if (num[j+0] == '1' && num[j+1] == '0' && num[j+2] == '1')
sum = 5;
else if (num[j+0] == '1' && num[j+1] == '1' && num[j+2] == '0')
sum = 6;
else if (num[j+0] == '1' && num[j+1] == '1' && num[j+2] == '1')
sum = 7;
/*当为000时省略*/
if (sum)
{
flag = 1;
}
if (flag)
{
printf("%d",sum);
}
}
printf("\n");
}
return 0;
}