题解 | #字符串合并处理#
字符串合并处理
http://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void sort(char *ptr)
{
int len = strlen(ptr);
for(int i = 0; i < len; i += 2)
{
for(int j = i+2; j < len; j += 2)
{
if(ptr[i] > ptr[j])
{
char ch = ptr[i];
ptr[i] = ptr[j];
ptr[j] = ch;
}
}
}
}
unsigned char str2hex(char ch)
{
if(isdigit(ch))
return ch - '0';
else
return tolower(ch) - 'a' + 10;
}
char hex2str(unsigned char hex)
{
if(hex < 10)
return '0' + hex;
else
return 'A' + hex - 10;
}
unsigned char convertch(unsigned char ch)
{
unsigned char ret = 0x00;
for(int i = 0; i < 4; i++)
{
if(ch & 0x1)
ret |= 0x1;
ch >>= 1;
ret <<= 1;
}
ret >>= 1;
return ret;
}
void convert(char *ptr)
{
int len = strlen(ptr);
for(int i = 0; i < len; i++)
{
if(isxdigit(ptr[i]))
ptr[i] = hex2str(convertch(str2hex(ptr[i])));
}
}
int main(void)
{
int i = 0;
char str[512], output[1024];
while(scanf("%s",str) != EOF)
{
if(i == 0)
{
memset(output, '\0', sizeof(output));
memcpy(output, str, strlen(str));
i++;
continue;
}
else
{
i = 0;
strcat(output, str);
sort(output);
sort(output+1);
// printf("%s\n", output);
convert(output);
printf("%s\n", output);
}
}
return 0;
}