华为机试-字符串排序

题目描述
编写一个程序,将输入字符串中的字符按如下规则排序。
规则 1 :英文字母从 A 到 Z 排列,不区分大小写。
如,输入: Type 输出: epTy
规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入: BabA 输出: aABb
规则 3 :非英文字母的其它字符保持原来的位置。
如,输入: By?e 输出: Be?y
样例:
输入:
A Famous Saying: Much Ado About Nothing(2012/8).
输出:
A aaAAbc dFgghh : iimM nNn oooos Sttuuuy (2012/8).

输入描述:

输出描述:

示例1
输入

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

A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).


问题分析:
该题保持顺序不变 其实就是利用稳定的排序方法,可以使用冒泡排序方法。交换条件需要仔细。

  1. import java.util.Scanner;  
  2.   
  3. /** 
  4.  * 题目描述 编写一个程序,将输入字符串中的字符按如下规则排序。 
  5.  */  
  6. public class Main {  
  7.     public static void main(String[] args) {  
  8.         Scanner scanner = new Scanner(System.in);  
  9.         while (scanner.hasNext()) {  
  10.             String string = scanner.nextLine();  
  11.             char[] ch = string.toCharArray();  
  12.             exchangeString(ch);  
  13.             String string2 = "";  
  14.             for (int i = 0; i < ch.length; i++) {  
  15.                 string2 += ch[i];  
  16.             }  
  17.             System.out.println(string2);  
  18.         }  
  19.   
  20.     }  
  21.   
  22.     private static void exchangeString(char[] cs) {  
  23.         int num = cs.length;  
  24.         int compare = 0;  
  25.         int j = num - 1;  
  26.         for (int i = 0; i < num; i++) {  
  27.             while (j > i) {  
  28.                 if ((cs[j] >= 'a' && cs[j] <= 'z') || (cs[j] >= 'A' && cs[j] <= 'Z')) {  
  29.                     compare = findNext(cs, j);// 找到下一个字母位置  
  30.                     if (compare != -1) {  
  31.                         int cha = cs[j] - cs[compare];  
  32.                         if (ifNeedExchange(cs[j], cs[compare])) {  
  33.                             char temp = cs[compare];  
  34.                             cs[compare] = cs[j];  
  35.                             cs[j] = temp;  
  36.                         }  
  37.                     } else {  
  38.                         break;  
  39.                     }  
  40.                     j = compare;  
  41.                 } else {  
  42.                     j--;  
  43.                     continue;  
  44.                 }  
  45.             }  
  46.             j = num - 1;  
  47.         }  
  48.   
  49.     }  
  50.   
  51.     private static boolean ifNeedExchange(char c, char d) {  
  52.         if (c >= 'a') {  
  53.             c -= 32;  
  54.         }  
  55.         if (d >= 'a') {  
  56.             d -= 32;  
  57.         }  
  58.   
  59.         return c < d;  
  60.     }  
  61.   
  62.     private static int findNext(char[] cs, int j) {  
  63.         if (j <= 0) {  
  64.             return -1;  
  65.         }  
  66.         for (int i = j - 1; i >= 0; i--) {  
  67.             if ((cs[i] >= 'a' && cs[i] <= 'z') || (cs[i] >= 'A' && cs[i] <= 'Z')) {  
  68.                 return i;  
  69.             }  
  70.         }  
  71.         return -1;  
  72.     }  
  73.   
  74. }  
原文地址:https://www.cnblogs.com/wwjldm/p/7218484.html