本程序时间和空间的把控,相对于大多数训练的同学来说,可以说是完美了,但是任有优化的余地。
我已经有了解决方案,但是,如果写的太好,反而埋没了同学们的天分。
直接给源码,不做批注,这是你们思考中,做的事情。
遇到,不定长的处理方式,我群里做了严谨的叙述,我希望大家,可以自己写出来。
#include <iostream>
#include <cstring>
using namespace std;
const char hexCode[16][5]={"0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001","1010","1011",
"1100","1101","1110","1111"
};
int main()
{
int num;
cin>>num;
auto hex=new char[num][2];
char *bin;
switch((8*num)%3)
{
case 0:
break;
case 1:
bin=new char[8*num+3]{0};
strcpy(bin,"00");
break;
case 2:
bin=new char[8*num+2]{0};
strcpy(bin,"0");
break;
}
for(int i=0;i<num;i++)
scanf("%s",&hex[i]);
for(int i=0;i<num;i++)
{
for(int j=0;j<2;j++)
{
switch(hex[i][j])
{
case '0':
strcat(bin,hexCode[0]);
break;
case '1':
strcat(bin,hexCode[1]);
break;
case '2':
strcat(bin,hexCode[2]);
break;
case '3':
strcat(bin,hexCode[3]);
break;
case '4':
strcat(bin,hexCode[4]);
break;
case '5':
strcat(bin,hexCode[5]);
break;
case '6':
strcat(bin,hexCode[6]);
break;
case '7':
strcat(bin,hexCode[7]);
break;
case '8':
strcat(bin,hexCode[8]);
break;
case '9':
strcat(bin,hexCode[9]);
break;
case 'A':
strcat(bin,hexCode[10]);
break;
case 'a':
strcat(bin,hexCode[10]);
break;
case 'B':
strcat(bin,hexCode[11]);
break;
case 'b':
strcat(bin,hexCode[11]);
break;
case 'C':
strcat(bin,hexCode[12]);
break;
case 'c':
strcat(bin,hexCode[12]);
break;
case 'D':
strcat(bin,hexCode[13]);
break;
case 'd':
strcat(bin,hexCode[13]);
break;
case 'E':
strcat(bin,hexCode[14]);
break;
case 'e':
strcat(bin,hexCode[14]);
break;
case 'F':
strcat(bin,hexCode[15]);
break;
case 'f':
strcat(bin,hexCode[15]);
break;
}
}
}
delete [] hex;
int i=3;
int temp=(bin[0]=='1'?4:0)+(bin[1]=='1'?2:0)+(bin[2]=='1'?1:0);
if(temp!=0)
printf("%d",temp);
while(bin[i])
{
temp=(bin[i]=='1'?4:0)+(bin[i+1]=='1'?2:0)+(bin[i+2]=='1'?1:0);
i+=3;
printf("%d",temp);
}
return 0;
}