题解 | #字符串合并处理#
字符串合并处理
http://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
//通俗易懂
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=null;
while((s=br.readLine())!=null){
String[] s1=s.split(" ");
String str=s1[0].concat(s1[1]);
char[] chars1=new char[(int)Math.ceil((double)(str.length()/2.0000))];//奇数的话,存放偶数的数组应该是str.length/2,向上取整
char[] chars2=new char[str.length()/2];
char[] chars=str.toCharArray();
for(int i=0;i<chars.length;i++){
if(i%2==0) chars1[i/2]=chars[i];
else if(i%2==1) chars2[i/2]=chars[i];
}
Arrays.sort(chars1);
Arrays.sort(chars2);
for(int i=0;i<chars.length;i++){
if(i%2==0) chars[i]=chars1[i/2];
else if(i%2==1) chars[i]=chars2[i/2];
}
//String ss2=String.valueOf(chars);
//System.out.println(ss2);
for(int i=0;i<chars.length;i++){
if(chars[i]=='0') chars[i]='0';
else if(chars[i]=='1') chars[i]='8';
else if(chars[i]=='2') chars[i]='4';
else if(chars[i]=='3') chars[i]='C';
else if(chars[i]=='4') chars[i]='2';
else if(chars[i]=='5') chars[i]='A';
else if(chars[i]=='6') chars[i]='6';
else if(chars[i]=='7') chars[i]='E';
else if(chars[i]=='8') chars[i]='1';
else if(chars[i]=='9') chars[i]='9';
else if(chars[i]=='a' || chars[i]=='A') chars[i]='5';
else if(chars[i]=='b' || chars[i]=='B') chars[i]='D';
else if(chars[i]=='c' || chars[i]=='C') chars[i]='3';
else if(chars[i]=='d' || chars[i]=='D') chars[i]='B';
else if(chars[i]=='e' || chars[i]=='E') chars[i]='7';
else if(chars[i]=='f' || chars[i]=='F') chars[i]='F';
}
String ss=String.valueOf(chars);
System.out.println(ss);
}
}
}