牛客网:读入一个字符串str,输出字符串str中的连续最长的数字

2020-07-12  本文已影响0人  编程小王子AAA

https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d?tpId=182&&tqId=34785&rp=1&ru=/activity/oj&qru=/ta/exam-all/question-ranking

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
 * 字符串中找出连续最长的数字串
 */
 
public class Main {
public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<str.length();i++){
        if(str.charAt(i)>='0' && str.charAt(i)<='9'){
            sb.append(str.charAt(i));
        }else{
            sb.append(" ");
        }
    }
    String s = sb.toString();
    String [] ss = s.split(" ");
    int a = ss[0].length();
    int b = 0;
    for(int i=1;i<ss.length;i++){
        if(ss[i].length()>a){
            a = ss[i].length();
            b= i;
        }else{
            continue;
        }
    }
    System.out.println(ss[b]);
}
}

上一篇下一篇

猜你喜欢

热点阅读