张德长

导航

牛客[编程题] HJ26 字符串排序

HJ26 字符串排序
中等  通过率:39.52%  时间限制:1秒  空间限制:32M
 

描述

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

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

如,输入: Type 输出: epTy

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

如,输入: BabA 输出: aABb

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

如,输入: By?e 输出: Be?y
 
 
数据范围:输入的字符串长度满足 1 \le n \le 1000 \

 

输入描述:

输入字符串

输出描述:

输出字符串

示例1

输入:
A Famous Saying: Much Ado About Nothing (2012/8).
输出:
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).

 

using System.Collections.Generic;
using System;
public class Program {
    public static void Main() {
        string line;
        while ((line = System.Console.ReadLine ()) !=
                null) { // 注意 while 处理多个 case
            List<int> abc = new List<int>();
            List<int> others = new List<int>();
            char[] res = new char[line.Length];
            char c;
            for (int i = 0; i < line.Length; i++) {
                c = line[i];
                if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
                    abc.Add(i);
                else {

                    others.Add(i);
                    res[i] = line[i];
                }
            }
            List<char> chars = new List<char>();
            for (int i = 0; i <= 'Z' - 'A'; i++) {
                for (int j = 0; j < abc.Count; j++) {
                    c = line[abc[j]];
                    if (c == 'A' + i || c == 'a' + i)
                        chars.Add(c);
                }

            }
            for (int i = 0; i < chars.Count; i++) {
                res[abc[i]] = chars[i];
            }
            for (int i = 0; i < res.Length; i++) {
                Console.Write(res[i]);
            }

        }
    }
}

 

posted on 2023-11-09 10:05  张德长  阅读(14)  评论(0编辑  收藏  举报