当然是利用Collections走捷径!把输入的字符串进行分割,数字串存入列表中。然后重写集合的排序接口,使之按照字符串的长度从大到小排列,最后只需要打印和第0个字符串长度相同的元素即可。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine();
List<String> list = new ArrayList<>();
String res="";
for(int i=0;i<str.length();++i){
if(Character.isDigit(str.charAt(i))){
res+=str.charAt(i);
}
else {
list.add(res);
res="";
}
}
if(!res.equals(""))list.add(res);
Collections.sort(list,new Comparator<String>(){
public int compare(String s1,String s2){
return s2.length()-s1.length();
}
});
int maxlen = list.get(0).length();
for(String s:list){
if(s.length()==maxlen){
System.out.print(s);
}
else break;
}
System.out.print(","+maxlen+"\n");
}
}
}