题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
//注意排序双重循环,单循环漏排;查表法实现替换。 #include <stdio.h> #include <string.h> int main() { char str1[101]={0}; char str2[101]={0}; char str[101]={0}; char input[] = {"0123456789abcdefABCDEF"}; char output[] = {"084C2A6E195D3B7F5D3B7F"}; char tmp; int len = 0; scanf( "%s", str1 ); scanf( "%s", str2 ); strcat(str,str1); strcat(str,str2); len = strlen(str); for(int i=0; i<len; i=i+2) { for(int j=i+2; j<len; j += 2) { if(str[i] > str[j]) { tmp = str[i]; str[i] = str[j]; str[j] = tmp; } } } //注意双重循环排序,单循环会漏排 for(int i=1; i<len; i=i+2) { for(int j=i+2; j<len; j += 2) { if(str[i] > str[j]) { tmp = str[i]; str[i] = str[j]; str[j] = tmp; } } } //查表实现转换 char outstr[202] = {'\0'}; for( int i=0; i<len; i++ ) { outstr[i] = str[i]; for( int j=0; j<22; j++ ) { if( str[i] == input[j] ) outstr[i] = output[j]; } } printf("%s\n", outstr); return 0; }