首页 > 试题广场 >

字符串排序

[编程题]字符串排序
  • 热度指数:275268 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

编写一个程序,将输入字符串中的字符按如下规则排序。

规则 1 :英文字母从 A 到 Z 排列,不区分大小写。

如,输入: Type 输出: epTy

规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。

如,输入: BabA 输出: aABb

规则 3 :非英文字母的其它字符保持原来的位置。

如,输入: By?e 输出: Be?y

数据范围:输入的字符串长度满足


输入描述:
输入字符串


输出描述:
输出字符串
示例1

输入

A Famous Saying: Much Ado About Nothing (2012/8).

输出

A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
不想看代码,脑子痛,所以直接说思路,大家记忆思路然后去做题

比如"cb?a"这个字符串,遍历这个字符串得到两个数组(或者链表)
数组S1:   cba 
数组S2:[ ] [ ] [ ?] [ ]
第二个数组S2相当于一个map,我记录下来了特殊字符的索引 (2) ,以及它的值( ?)

然后先排序cba得到abc,接着把问好插入到索引2位置,得到 ab?c

接着遍历那个
发表于 2024-04-11 10:48:29 回复(0)
我发现大家的解法各不相同,我也贴一个我的:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String val = in.nextLine();
        char[] cs = val.toCharArray();
        Map<Integer, String> map = new HashMap();
        List<Character> li = new ArrayList();
        for (int i = 0 ; i < cs.length; i++) {
            if ((cs[i] < 'a' || cs[i] > 'z') && (cs[i] < 'A' || cs[i] > 'Z')) {
                map.put(i, String.valueOf(cs[i]));
            } else {
                li.add(cs[i]);
            }
        }
        li.sort(new Comparator<Character>() {
            public int compare(Character o1, Character o2) {
                return Character.toLowerCase(o1) - Character.toLowerCase(o2);
            }
        });
        StringBuilder result = new StringBuilder();
        for (int i = 0; result.length() < val.length(); ) {
            if (map.containsKey(result.length())) {
                result.append(map.get(result.length()));
            } else if (i < li.size()) {
                result.append(li.get(i));
                i++;
            }
        }
        System.out.print(result.toString());
    }
}


编辑于 2024-04-07 17:49:09 回复(0)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        List<Character> list = new ArrayList<>();
        for(char c: str.toCharArray()){
            if(Character.isLetter(c)){
                list.add(c);
            }
        }
        list.sort(new Comparator<Character>(){
            public int compare(Character o1, Character o2){
                return Character.toLowerCase(o1)- Character.toLowerCase(o2);
            }
        });
        StringBuilder builder = new StringBuilder();
        for(int i=0,j=0;i<str.length();i++){
            if(Character.isLetter(str.charAt(i))){
                builder.append(list.get(j++));
            } else {
                builder.append(str.charAt(i));
            }
        }
        System.out.println(builder.toString());
    }
}

编辑于 2024-03-08 18:57:12 回复(1)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String s = in.nextLine();
char[] chars = s.toCharArray();

int len = s.length();
//冒泡排序
for (int i = 0; i < len - 1 ; i++) {
for (int j = 0 ; j < len - 1; j++) {
if (!Character.isLetter(chars[j]))
continue;
int k = j + 1;
//双指针查找后续英文字符做交换,非英文字符跳过
while ( k < len && !Character.isLetter(chars[k]))
k++;
//k = len时说明,后面的都是从第j+1个元素到最后一个元素都是非英文字符,跳出循环
if (k == len)
break;
int a = Character.toLowerCase(chars[j]);
int b = Character.toLowerCase(chars[k]);
if (b < a) {
char tmp = chars[k];
chars[k] = chars[j];
chars[j] = tmp;
}
}
}
for (char c : chars) {
System.out.print(c);
}
}
}
}
编辑于 2023-12-22 14:29:07 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            char[] charArray = in.nextLine().toCharArray();
            LinkedHashMap<Integer, Character> map = new LinkedHashMap<>();
            ArrayList<Character> arr = new ArrayList<>();
            //将非字母字符存入map中,字母字符存入ArrayList中
            for (int i = 0; i < charArray.length; i++) {
                if ((charArray[i] < 'A' || charArray[i] > 'z') ||((charArray[i] > 'Z') && charArray[i] < 'a' )  ){
                    map.put(i,charArray[i]);
                    continue;
                }
               arr.add(charArray[i]);
            }
            //对ArrayList中的字母忽视大小写排序
            Collections.sort(arr, new Comparator<Character>() {
                @Override
                public int compare(Character x, Character y) {
                    return Character.compare(Character.toLowerCase(x), Character.toLowerCase(y));
                }
            });
            //对结果进行拼接
            StringBuffer stringBuffer = new StringBuffer();
            int index = 0;
            for (int i = 0; i < charArray.length; i++) {
                if (map.get(i) != null  &&map.get(i) == charArray[i]){
                    stringBuffer.append(charArray[i]);
                }else {
                    stringBuffer.append(arr.get(index));
                    index++;
                }
            }
            System.out.println(stringBuffer);
        }
    }
}

发表于 2023-12-14 16:44:23 回复(0)
import java.util.Scanner;
请问一下这段代码无法保持
大小写字母的相对顺序
应该怎么修改呢
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
   public static void main(String[] args) {
       // 创建一个 Scanner 对象,用于读取用户输入
       Scanner in = new Scanner(System.in);

       // 读取用户输入的一行字符,并将其转换为字符数组
       char[] cs = in.nextLine().toCharArray();

       // 创建一个整型数组,用于存储字符数组中每个字符的大小写标志(1表示大写,0表示小写)
       int[] flg = new int[cs.length];

       // 遍历字符数组,将小写字母转换为大写字母,并设置对应的标志位
       for (int i = 0; i < cs.length; i++) {
           if (cs[i] > 96 && cs[i] < 123) {
               flg[i] = 1;
               cs[i] -= 32;
           }
       }

       // 对字符数组进行排序,同时保持大小写字母的相对顺序
       for (int i = 0; i < cs.length; i++) {
           for (int j = i + 1; j < cs.length; j++) {
               if ((cs[i] >= 65 && cs[i] <= 90) && (cs[j] >= 65 && cs[j] <= 90)) {
                   if (cs[i] - cs[j] > 0) {
                       if (flg[i] != flg[j]) {
                           int y = flg[i];
                           flg[i] = flg[j];
                           flg[j] = y;
                       }
                       char x = cs[i];
                       cs[i] = cs[j];
                       cs[j] = x;
                   }
               }
           }
       }

       // 如果字符原来是大写的,将其转换回大写
       for (int i = 0; i < cs.length; i++) {
           if (flg[i] == 1) {
               cs[i] += 32;
           }
       }

       // 输出排序后的字符数组
       System.out.println(String.valueOf(cs));
   }
}
发表于 2023-11-30 14:08:43 回复(0)
    public static String sortWords(String words) {
        char[] c1 = words.toCharArray();
        int k = 0;
        Character[] c = words.replaceAll("[^a-zA-Z]", "")
                        .chars ()
                        .mapToObj (a -> (char) a)
                        .toArray (Character []::new);
        Arrays.sort (c, (x, y) -> {
                return Character.toLowerCase (x) - Character.toLowerCase (y);
        });
        for (int i = 0 ; i < c1.length; i++) {
            if (Character.isLetter(c1[i])) {
                c1[i] = c[k++];
            }
        }
        return new String(c1);
    }

发表于 2023-10-16 00:19:08 回复(0)
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();

        String[] strs = new String[str.length()];
        for (int i = 0; i < str.toCharArray().length; i++) {
            strs[i] = String.valueOf(str.toCharArray()[i]);
        }

        LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            if (!strs[i].matches("[A-Za-z]")) {
                map.put(i, strs[i]);
            } else {
                list.add(strs[i]);
            }
        }

        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size() - 1 - i; j++) {
                String tmp = "";
                if (list.get(j).compareToIgnoreCase(list.get(j + 1)) > 0) {
                    tmp = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, tmp);
                }
            }
        }

        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> next = iterator.next();
            list.add(next.getKey(), next.getValue());
        }

        for (String s : list) {
            System.out.print(s);
        }
    }
}

发表于 2023-09-17 21:57:11 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String str=in.nextLine();
        int len=0;
        for(int i=0;i<str.length();i++){
            if((str.charAt(i)>='A'&&str.charAt(i)<='Z')||
                    (str.charAt(i)>='a'&&str.charAt(i)<='z')){
                len++;
            }
        }
        String arr[]=new String[len];
        int l=0;
        for(int i=0;i<str.length();i++){
            if((str.charAt(i)>='A'&&str.charAt(i)<='Z')||
                    (str.charAt(i)>='a'&&str.charAt(i)<='z')){
                arr[l]=String.valueOf(str.charAt(i));
                l++;
            }
        }
        String arr2[]=new String[len];
        int index=0;
        for(int i=65;i<=90;i++){
            for(int j=0;j<len;j++){
                if(arr[j].charAt(0)==(char)i||arr[j].charAt(0)==(char)(i+32)){
                    arr2[index]=arr[j];
                    index++;
                }
            }
        }
        int flag=0;
        for(int i=0;i<str.length();i++){
            if((str.charAt(i)>='A'&&str.charAt(i)<='Z')||
                    (str.charAt(i)>='a'&&str.charAt(i)<='z')){
                System.out.print(arr2[flag]);
                flag++;
            }else {
                System.out.print(str.charAt(i));
            }
        }
    }
}

发表于 2023-09-16 01:43:28 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        char[] chars = line.toCharArray();
        StringBuilder sb = new StringBuilder();
        // 按照排序后的字符顺序,在字符串中寻找,如果找到,则添加到sb中
        for (int i = 'A'; i <= 'Z'; i++) {
            char c = (char) i;
            for (int j = 0; j < chars.length; j++) {
                if (c == chars[j] || chars[j] == c - 'A' + 'a') {
                    sb.append(chars[j]);
                }
            }
        }

        // 非字符的插入到对应的位置上
        for (int i = 0; i < chars.length; i++) {
            if (!((chars[i] >= 'a' && chars[i] <= 'z') || (chars[i] >= 'A' &&
                    chars[i] <= 'Z'))) {
                sb.insert(i, chars[i]);
            }
        }

        // 打印输出
        System.out.println(sb.toString());
    }
}

发表于 2023-08-07 16:16:26 回复(0)
/**
* 基数排序+位图
*/
import java.util.Comparator;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        java.util.List<String> list = new java.util.ArrayList<>();
        java.util.List<String> listNoAlpher = new java.util.ArrayList<>();
        Tuple[] tuples = new Tuple[26];
        String copy = "";
        boolean[] bitmap = new boolean[str.length()];
        for(int i=0;i<str.length();i++){
            char c = str.charAt(i);
            boolean lower = c>='a'&&c<='z';
            boolean upper = c>='A'&&c<='Z';
            if(lower||upper){
                bitmap[i] = false;
                if(upper){
                    c = (char)(c+32);
                }
                int index = c-97;
                if(tuples[index]==null){
                    tuples[index] = new Tuple();
                }
                tuples[index].list.add(String.valueOf(str.charAt(i)));
                continue;
            }
            listNoAlpher.add(String.valueOf(str.charAt(i)));
            bitmap[i] = true;
        }
        java.util.List<String> orderList = new java.util.ArrayList<>();
        for(Tuple t:tuples){
            if(t==null){
                continue;
            }
            for(String s:t.list){
                orderList.add(s);
            }
        }
        String s="";
        for(int i=0;i<bitmap.length;i++){
            if(!bitmap[i]){
                s+=orderList.remove(0);
            }else{
                s+=listNoAlpher.remove(0);
            }
        }
        System.out.println(s);
        in.close();
    }
    static class Tuple{
        java.util.List<String> list = new java.util.ArrayList<>();
    }
}


发表于 2023-07-21 16:35:49 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
         public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();

        char[] arr = str.toCharArray();
        // 存放排序的char
        LinkedList<Character> list = new LinkedList<>();
        // 存放特殊字符的 下标以及字符
        Map<Integer, Character> map =  new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            // 确定 是大小写字母
            if (('A' <= arr[i] && arr[i] <= 'Z') || ('a' <= arr[i] && arr[i] <= 'z')) {
                // 如果 list长度为0 直接方第一个
                if (list.size() == 0) {
                     list.add(0, arr[i]);
                    continue;
                }

                // 将大写字母转变为小写字母 (也可以是将小写字母转变为大写字母)
                char c = arr[i];
                if ('A' <= c && c <= 'Z') {
                    c = (char) (c + 32);
                }

                // 从后向前 (也可以是从前往后)
                for (int j= list.size() -1; j >= 0; j--) {

                    // 将List中的大写字母转变为小写字母
                    char c1 = list.get(j);
                    if ('A' <= c1 && c1 <= 'Z') {
                        c1 = (char) (c1 + 32);
                    }

                    // arr 和 list比大小 进行存放
                    if (c >= c1) {
                        int n = 1;
                        list.add(j+n, arr[i]);
                        break;
                    } else if (j == 0 && c1 > c) { 
                        // 如果比较到 j == 0 也没有找到c >= c1
                        // 说明c1最小直接放在第一位
                        list.addFirst(arr[i]);
                    }
                }
            } else {
                // 非 大小写字母进行存储 map
                map.put(i, arr[i]);
            }
        }

        // 特殊字符按照下标排序
        List<Integer> keyList = new ArrayList<>(map.keySet());
        keyList.sort((a, b) -> a.compareTo(b));

        // 将特殊字符放到之前下标的位置  
        keyList.forEach(index -> {
            list.add(index, map.get(index));
        });

        // 输出
        StringBuilder builder = new StringBuilder();
        list.forEach( item -> {
            builder.append(item);
        });

        System.out.print(builder.toString());
    }
}

发表于 2023-07-15 15:12:10 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringBuilder str = new StringBuilder(in.nextLine());
        //存在乱序问题
        // for (int a = 0; a < str.length(); a++) {
        //     for (int i = 0; i < str.length() - 1; i++) {
        //         Character ch1 = str.charAt(i);
        //         if (!Character.isLetter(ch1)) {
        //             continue;
        //         }
        //         ch1 = Character.toLowerCase(ch1);
        //         for (int j = i + 1; j < str.length(); j++) {
        //             Character ch2 = str.charAt(j);
        //             if (!Character.isLetter(ch2)) {
        //                 continue;
        //             }
        //             ch2 = Character.toLowerCase(ch2);
        //             if (ch2 < ch1) {
        //                 Character t = str.charAt(i);
        //                 str.setCharAt(i, str.charAt(j));
        //                 str.setCharAt(j, t);
        //             }
        //         }
        //     }
        // }
        for (int a = 0; a < str.length(); a++) {
            for (int i = 0; i < str.length() - 1; i++) {
                Character ch1 = str.charAt(i);
                if (!Character.isLetter(ch1)) {
                    continue;
                }
                //找到前一个字母 继续找之后的第一个字母
                for (int j = i + 1; j < str.length(); j++) {
                    Character ch2 = str.charAt(j);
                    if (!Character.isLetter(ch2)) {
                        //没有找到之后的第一个字母,继续
                        continue;
                    } else {
                        //找到了之后的第一个字母
                        ch1 = Character.toLowerCase(ch1);
                        ch2 = Character.toLowerCase(ch2);
                        if (ch2 < ch1) {
                            //如果后面的小 交换
                            Character t = str.charAt(i);
                            str.setCharAt(i, str.charAt(j));
                            str.setCharAt(j, t);
                        }
                        break;
                    }

                }


            }
        }
        System.out.println(str);
    }
}

发表于 2023-07-12 08:07:21 回复(0)
import java.util.Scanner;
import java.util.LinkedHashMap;
import java.util.ArrayList;
import java.util.Set;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String input = in.nextLine();

            // LinkedHashMap用来存放非英文字母和它的初始位置
            LinkedHashMap<Integer, Character> map = new LinkedHashMap<Integer, Character>();
            // ArrayList用来存放英文字母,使用类冒泡排序进行排序(认为字母大小写相等)
            ArrayList<Character> list = new ArrayList<Character>();

            for (int i = 0; i < input.length(); i++) {
                char tempCh = input.charAt(i);
                if ((tempCh >= 'A' && tempCh <= 'Z') || (tempCh >= 'a' && tempCh <= 'z')) {
                    list.add(Character.valueOf(tempCh));
                } else {
                    map.put(Integer.valueOf(i), Character.valueOf(tempCh));
                }
            }

            // System.out.println(list);

            //冒泡排序
            int length = list.size();
            for (int i = 0; i < length - 1; i++) {

                for (int j = 0; j < length - 1 - i; j++) {
                    Character tempC = null;
                    if (compare(list.get(j), list.get(j + 1)) > 0) {
                        tempC = list.get(j);
                        list.set(j, list.get(j + 1));
                        list.set(j + 1, tempC);
                    }
                }
            }

            // System.out.println(list);
            // System.out.println(map);

            Set<Integer> TS = map.keySet();
            for (Integer i : TS) {
                int n = i.intValue();
                list.add(n, map.get(i));
            }
            //  System.out.println(list);
            for (int i = 0; i < list.size(); i++) {
               System.out.print(list.get(i));
                }

        }
    }

    // 比较两个字母大小,c1>c2,返回正数,c1=c2,返回0,c1<c2,返回负数
    //规定  A=a  A<b  B>a a<C
    public static int compare(Character c1, Character c2) {
        int result;
        char ch1 = c1.charValue();
        char ch2 = c2.charValue();

        //将小写字符转换为大写字符
        if (ch1 >= 'a' && ch1 <= 'z') {
            ch1 = (char)(ch1 - 32);
        }
        if (ch2 >= 'a' && ch2 <= 'z') {
            ch2 = (char)(ch2 - 32);
        }

        return ch1 - ch2;
    }
}

发表于 2023-07-03 22:13:21 回复(0)
public class Test04 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            char[] chars = in.nextLine().toCharArray();
            char[] result = new char[chars.length];
            LinkedList<Character> list = new LinkedList<>();

            //将字母放入list中,非字母按原位置放入result数组中
            for (int i = 0; i < chars.length; i++) {
                if(!Character.isLetter(chars[i])){
                    result[i] = chars[i];
                }else{
                    list.add(chars[i]);
                }
            }
            //将字母排序:按照小写顺序排序,字母相同,list本身按照输入顺序添加,位置不变。
            list.sort(new Comparator<Character>() {
                @Override
                public int compare(Character o1, Character o2) {
                    return Character.toLowerCase(o1)-Character.toLowerCase(o2);
                }
            });
            //将排序好的字母按顺序放入result数组中。char[]初始值为0。
            for (int i = 0; i < result.length; i++) {
                if(result[i]==0){
                    result[i]= list.pop();
                }
            }
            System.out.println(new String(result));
        }
    }
}

发表于 2023-06-02 17:09:58 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String string = in.nextLine();
            int length = string.length();
            String[] strs = new String[26];
            String[] cs = new String[length];
            for (int i = 0; i < length; i++) {
                char c = string.charAt(i);
                String s = string.substring(i, i + 1);
                String xs = s.toLowerCase();
                char[] chars = xs.toCharArray();
                int index = chars[0] - 'a';
                if (Character.isLetter(c)) {
                    strs[index] = strs[index] == null ? s : strs[index] + s;
                } else {
                    cs[i] = s;
                }
            }

            String s = "";
            for (int i = 0; i < strs.length; i++) {
                String str = strs[i];
                if (str != null) {
                    s = s == "" ? strs[i] : s + strs[i];
                }
            }

            int i = 0;
            while (i < length) {
                String str = cs[i];
                if (str != null) {
                    String s1 = s.substring(0, i);
                    String s2 = s.substring(i, s.length());
                    s = s1 + str + s2;
                }
                i++;
            }

            System.out.print(s);
        }
    }
}


发表于 2023-05-27 17:04:39 回复(0)
// 使用插入法解题
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            String b = new String();
            int length = a.length();
            StringBuilder sb = new StringBuilder();
            // 收集所有的字母
            for (int i = 0; i < length; i++) {
                char c = a.charAt(i);
                if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) {
                    int aaa = (c > 'Z') ? c - 'a' : c - 'A';
                    int lengthB = b.length();
                    int j = 0;
                    // 收集字母的时候顺便排序,使用插入法
                    for (; j < lengthB; j++) {
                        char d = b.charAt(j);
                        int bbb = (d > 'Z') ? d - 'a' : d - 'A';
                        if (bbb > aaa) {
                            String str1 = b.substring(0, j);
                            String str2 = b.substring(j);
                            sb.append(str1);
                            sb.append(c);
                            sb.append(str2);
                            b = sb.toString();
                            sb = new StringBuilder();
                            break;
                        }
                    }
                    if (j >= lengthB) {
                        b = b + c;
                    }
                }
            }
            // 输出,字母选择排序后的字符串中的字母,特殊字符直接输出
            for (int i = 0, j = 0; i < length; i++) {
                char c = a.charAt(i);
                if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) {
                    System.out.printf(String.valueOf(b.charAt(j)));
                    j++;
                } else {
                    if ('%' == c) {
                        System.out.printf("%%");
                    } else {
                        System.out.printf(String.valueOf(c));
                    }
                }
            }
            System.out.printf("\n");
        }
    }
}

发表于 2023-05-23 11:52:15 回复(0)