张德长

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

牛客[编程题] 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   张德长  阅读(24)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示