2017华工软院机试第一题

输入一个字符串,要求输出能把所有的小写字符放前面,大写字符放中间,数字放后面,并且中间用空格隔开,如果同种类字符间有不同种类的字符,输出后也要用字符隔开

例如:

输入 12abc3KF12

输出 abc KF 12 3 12

输入 rwr21r3hello666world

输出 rwr r hello world 21 3 666

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.wereash.scut_hot100;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* @Author: WereAsh
* @Date:2026-02-25 21:21
**/
public class Solution2017 {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
String s=scanner.nextLine();
int n=s.length();
if(n==1){
System.out.println(s);
}
List<String> intList=new ArrayList<>();
List<String> lowList=new ArrayList<>();
List<String> upList=new ArrayList<>();
int end=0;
while(end<n){
int start=end;
while (end<n&&Character.isDigit(s.charAt(end))){
end++;
}
if(start!=end){
intList.add(s.substring(start,end));
}
start=end;
while (end<n&&Character.isLowerCase(s.charAt(end))){
end++;
}
if(start!=end){
lowList.add(s.substring(start,end));
}
start=end;
while (end<n&&Character.isUpperCase(s.charAt(end))){
end++;
}
if(start!=end){
upList.add(s.substring(start,end));
}
}
StringBuilder sb=new StringBuilder();
for(int i=0;i<lowList.size();i++){
sb.append(lowList.get(i));
sb.append(' ');
}
for (int i=0;i<upList.size();i++){
sb.append(upList.get(i));
sb.append(' ');
}
for(int i=0;i<intList.size();i++){
sb.append(intList.get(i));
if(i<intList.size()-1){
sb.append(' ');
}
}
System.out.println(sb.toString());
}
}