牛客网--字符串合并处理(Java)
按照指定規則對輸入的字符串進行處理。
詳細描述:
將輸入的兩個字符串合并。
對合并后的字符串進行排序,要求為:下標為奇數的字符和下標為偶數的字符分別從小到大排序。這里的下標意思是字符在字符串中的位置。
對排序后的字符串進行操作,如果字符為‘0’——‘9’或者‘A’——‘F’或者‘a’——‘f’,則對他們所代表的16進制的數進行BIT倒序的操作,并轉換為相應的大寫字符。如字符為‘4’,為0100b,則翻轉后為0010b,也就是2。轉換后的字符為‘2’;?如字符為‘7’,為0111b,則翻轉后為1110b,也就是e。轉換后的字符為大寫‘E’。
?
舉例:輸入str1為"dec",str2為"fab",合并為“decfab”,分別對“dca”和“efb”進行排序,排序后為“abcedf”,轉換后為“5D37BF”
接口設計及說明:
/*
功能:字符串處理
輸入:兩個字符串,需要異常處理
輸出:合并處理后的字符串,具體要求參考文檔
返回:無
*/
void?ProcessString(char*?str1,char?*str2,char?*?strOutput)
{
}
?
?
輸入描述:
輸入兩個字符串
輸出描述:
輸出轉化后的結果
示例1
輸入
復制
dec fab輸出
復制
5D37BF?
代碼:
import java.util.*;
public class Main{
? ? public static void main(String[] args){
? ? ? ? Scanner sc = new Scanner(System.in);
? ? ? ? while(sc.hasNext()){
? ? ? ? String x = sc.next();
? ? ? ? String y = sc.next();
? ? ? ? x = x+y;
? ? ? ? String string1="";
? ? ? ? String string2="";
? ? ? ? for(int i=0;i<x.length();i++){
? ? ? ? ? ? if(i%2==0){
? ? ? ? ? ? ? ? string1+=x.charAt(i);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? string2+=x.charAt(i);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ?char[] str1=string1.toCharArray();
? ? ? ? ?char[] str2=string2.toCharArray();
? ? ? ? ?Arrays.sort(str1);
? ? ? ? ?Arrays.sort(str2);
? ? ? ? String str="";
? ? ? ? int k=0;
? ? ? ? for(int i=0;i<Math.min(str1.length,str2.length);i++){
? ? ? ? ? ? str+=str1[i];
? ? ? ? ? ? str+=str2[i];
? ? ? ? ? ? if(i==Math.min(str1.length,str2.length)-1){
? ? ? ? ? ? ? ? ? ? ?k=i;
? ? ? ? ? ? ? ? ?}
? ? ? ? }
? ? ? ? if(str1.length>str2.length){
? ? ? ? ? ? str+=str1[k+1];
? ? ? ? }else if(str1.length<str2.length){
? ? ? ? ? ? str+=str2[k+1];
? ? ? ? }
? ? ? ? StringBuffer sb = new StringBuffer();
? ? ? ?// System.out.println(str);
? ? ? ? for(int i=0;i<str.length();i++){
? ? ? ? ? ? int a = helper(str.charAt(i));
? ? ? ? ? ? if(a<=15)
? ? ? ? ? ? ?? ?sb.append(func(a));
? ? ? ? ? ? else
? ? ? ? ? ? ?? ?sb.append((char)(a));
? ? ? ? ? ??
? ? ? ? }
? ? ? ? System.out.println(sb.toString());
? ? ? ? }
? ? ? ?
? ? }
? ? public static String func(int x) {
? ? ?? ? if(x==10){
? ? ? ? ? ? ?return "A";
? ? ? ? ?}else if(x==11){
? ? ? ? ? ? ?return "B";
? ? ? ? ?}else if(x==12){
? ? ? ? ? ? ?return "C";
? ? ? ? ?}else if(x==13){
? ? ? ? ? ? ?return "D";
? ? ? ? ?}else if(x==14){
? ? ? ? ? ? ?return "E";
? ? ? ? ?}else if(x==15){
? ? ? ? ? ? ?return "F";
? ? ? ? ?}
? ? ? ?return String.valueOf(x);
? ? }
? ? public static int helper(char x){
? ? ? ? if(x>='0'&&x<='9'){
? ? ? ? ? ? int result=0;
? ? ? ? ? ? result=(int)(x-'0');
? ? ? ? ? ? int a=0,b=0,c=0,d=0;
? ? ? ? ? ? if(2*2*2<=result){
? ? ? ? ? ? ? ? result-=2*2*2;
? ? ? ? ? ? ? ? a=1;
? ? ? ? ? ? }if(2*2<=result){
? ? ? ? ? ? ? ? result-=2*2;
? ? ? ? ? ? ? ? b=1;
? ? ? ? ? ? }if(2<=result){
? ? ? ? ? ? ? ? result-=2;
? ? ? ? ? ? ? ? c=1;
? ? ? ? ? ? }if(result>=1){
? ? ? ? ? ? ? ? result-=1;
? ? ? ? ? ? ? ? d=1;
? ? ? ? ? ? }
? ? ? ? ? ? result=2*2*2*d+2*2*c+2*b+a;
? ? ? ? ? ? return result;
? ? ? ? }else if(x>='a'&&x<='f'){
? ? ? ? ? ? int result=0;
? ? ? ? ? ? result=(int)(x-'a'+10);
? ? ? ? ? ? int a=0,b=0,c=0,d=0;
? ? ? ? ? ? if(2*2*2<=result){
? ? ? ? ? ? ? ? result-=2*2*2;
? ? ? ? ? ? ? ? a=1;
? ? ? ? ? ? }if(2*2<=result){
? ? ? ? ? ? ? ? result-=2*2;
? ? ? ? ? ? ? ? b=1;
? ? ? ? ? ? }if(2<=result){
? ? ? ? ? ? ? ? result-=2;
? ? ? ? ? ? ? ? c=1;
? ? ? ? ? ? }if(result>=1){
? ? ? ? ? ? ? ? result-=1;
? ? ? ? ? ? ? ? d=1;
? ? ? ? ? ? }
? ? ? ? ? ? result=2*2*2*d+2*2*c+2*b+a;
? ? ? ? ? ? return result;
? ? ? ? }else if(x>='A'&&x<='F'){
? ? ? ? ? ? int result=0;
? ? ? ? ? ? result=(int)(x-'A'+10);
? ? ? ? ? ? int a=0,b=0,c=0,d=0;
? ? ? ? ? ? if(2*2*2<=result){
? ? ? ? ? ? ? ? result-=2*2*2;
? ? ? ? ? ? ? ? a=1;
? ? ? ? ? ? }if(2*2<=result){
? ? ? ? ? ? ? ? result-=2*2;
? ? ? ? ? ? ? ? b=1;
? ? ? ? ? ? }if(2<=result){
? ? ? ? ? ? ? ? result-=2;
? ? ? ? ? ? ? ? c=1;
? ? ? ? ? ? }if(result>=1){
? ? ? ? ? ? ? ? result-=1;
? ? ? ? ? ? ? ? d=1;
? ? ? ? ? ? }
? ? ? ? ? ? result=2*2*2*d+2*2*c+2*b+a;
? ? ? ? ? ? return result;
? ? ? ? }
? ? ? ?return (int)(x);
? ? }
}
總結
以上是生活随笔為你收集整理的牛客网--字符串合并处理(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode--738. 单调递增的
- 下一篇: Leetcode--904. 水果成篮