题目描述
读入一个字符串str,输出字符串str中的连续最长的数字串
输入描述:
个测试输入包含1个测试用例,一个字符串str,长度不超过255。
输出描述:
在一行内输出str中里连续最长的数字串。
示例1
输入
abcd12345ed125ss123456789
输出
123456789
解题思路:用max表示经过的数字长度最大值,count表示数字计数器,当为字母时重置为0,end表示数字尾部,每次满足数字时,对max进行判断,当max小于于count时,更新max和end
public class Test0531 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String str = scanner.nextLine();
int max = 0;
int count=0;
int end=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>='0' && str.charAt(i)<='9'){
count++;
if(max<count){
max= count;
end = i;
}
}else{
count = 0;
}
}
System.out.println(str.substring(end-max+1,end+1));
}
}
}
博客给出一个编程题目,要求读入字符串,输出其中连续最长的数字串,并给出输入输出示例。同时介绍了解题思路,用max记录数字长度最大值,count作数字计数器,end标记数字尾部,遇到字母时count重置,满足数字时更新max和end。
9422

被折叠的 条评论
为什么被折叠?



