题解 | #字符串合并处理#
字符串合并处理
http://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
#include <stdio.h>
#include <string.h>
static char g_plain[] = {"0123456789abcdefABCDEF"};
static char g_cipher[] = {"084C2A6E195D3B7F5D3B7F"};
static char g_string[201] = {0};
int main(int argc, char** argv)
{
while (gets(g_string) != NULL) {
int len = strlen(g_string);
if (len == 0) continue;
for (int i = 0; i < len; i++) { /* 字符串合并 */
if (g_string[i] != ' ') continue;
for (int j = i; j < len - 1; j++) g_string[j] = g_string[j + 1];
g_string[--len] = 0; i--;
}
for (int i = 0; i < len - 2; i += 2) { /* 排序 */
if (g_string[i] < g_string[i + 2]) continue;
for (int j = i + 2; j > 1; j -= 2) {
if (g_string[j] >= g_string[j - 2]) break;
g_string[j] ^= g_string[j - 2] ^= g_string[j] ^= g_string[j - 2];
}
}
for (int i = 1; i < len - 2; i += 2) {
if (g_string[i] < g_string[i + 2]) continue;
for (int j = i + 2; j > 2; j -= 2) {
if (g_string[j] >= g_string[j - 2]) break;
g_string[j] ^= g_string[j - 2] ^= g_string[j] ^= g_string[j - 2];
}
}
for (int i = 0; i < len; i++) for (int j = 0; j < sizeof(g_plain); j++) {
if (g_string[i] == g_plain[j]) { g_string[i] = g_cipher[j]; break; }
}
printf("%s\n", g_string);
}
return 0;
}