import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String ret = ""; String max = ""; int len = str.length(); int i = 0; for(; i < len; i++){ char ch = str.charAt(i); if(ch >= '0' && ch <= '9'){ ret = ret + ch + ""; }else{ if(ret.length() > max.length()){ max = ret; }else{ ret = ""; } } } //处理最后一个字符串是数字的情况,如果是数字则循环进不来 if(i == len && ret.length() > max.length()){ max = ret; } System.out.println(max); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String cur = ""; String ret = ""; int i = 0; for( ;i < str.length();i++){ char ch = str.charAt(i); if(ch >= '0' && ch <= '9'){ cur = cur + ch +""; }else{ if(cur.length() > ret.length()){ ret = cur; }else{ cur = ""; } } } if(i == str.length() && cur.length() > ret.length()){ ret = cur; } System.out.println( ret); } }
public static void main(String[] args) { ArrayList <StringBuffer>strlsit = new ArrayList<>(); String stri ="abcd12345ed125ss123456789"; StringBuffer str = new StringBuffer(); int size=0,max=0; for(int i=0;i<stri.length();i++) { if(stri.charAt(i)>='0'&&stri.charAt(i)<='9') { str.append(stri.charAt(i)); size++; }else { str = new StringBuffer(); size=0; } } max=size>max?size:max; strlsit.add(str); for(StringBuffer s:strlsit) { if(s.length()==max) { System.out.println(s); } }
import java.util.Scanner; public class Main { //获取最长的连续数字串 public static String maxDigitalStr(String str){ char[] arrayChar = str.toCharArray();//将str转换成字符数组 int maxDigiStrLen = 0;//最长连续数字串的长度 String maxDigiStr = "";//最长连续数字串 int tempStrLen = 0;//临时数字串长度 StringBuffer tempStr = new StringBuffer();//临时数字串 for(int i = 0; i < arrayChar.length; i++){ //过滤掉非数字的字符 if(arrayChar[i] >= '0' && arrayChar[i] <= '9'){ tempStr.append(arrayChar[i]); tempStrLen++; if(tempStrLen > maxDigiStrLen){ maxDigiStrLen = tempStrLen;//更新 最长连续数字串长度 maxDigiStr = tempStr.toString();//更新 最长连续数字串 } }else{ tempStrLen = 0;//临时数字串长度 置0 tempStr.setLength(0);//临时数字串 清空 } } return maxDigiStr; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String tempStr = sc.next(); System.out.println(maxDigitalStr(tempStr)); } }
//换个思路,用正则相关的API
public static void way(String str){
Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher(str);
String maxsub="0"; //随便给的值,为了不报空指向异常
while (matcher.find()){
String group = matcher.group();
if(maxsub.length()<group.length()){
maxsub=group;
}
}
System.out.println(maxsub);
}
import java.util.*; public class Main { public static String theLongestNum(String str) { String numStr="123456789"; while (numStr.length()!=0) { if (str.contains(numStr)) return numStr; else numStr=numStr.substring(0,numStr.length()-1); } return ""; } public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.println(theLongestNum(in.nextLine())); } }
import java.util.*; public class Main { public static String theLongestNum(String str) { int max=0; int length=0; int end=0;//用于指示数字串最末 char[] strToChar=str.toCharArray(); for(int i=0;i<strToChar.length;i++) { //遇到数字 if(str.charAt(i)>='0'&&str.charAt(i)<='9') { length++; if(length>max) {max=length; //换max值时end下标需要跟过来 end=i; } } //不是数字,需要length重新赋为0 else {length=0;} } //有max个数。 return str.substring(end-max+1,end+1); } public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.println(theLongestNum(in.nextLine())); } }
//输出字符串中连续最长的数字串 #include <cctype> #include <string> #include <iostream> using namespace std; int main() { int maxLength = 0; string s; cin>>s; int len = s.size(); if(len==0) {cout<<""<<endl;return 0;} int lenNow = 0; string str = ""; string result; for(int j=0;j<len;j++) { if(isdigit(s[j])) { str+=s[j]; lenNow = str.length(); if(lenNow>maxLength) { maxLength = lenNow; result = str; } } else { str=""; } } cout<<result<<endl; }
/* 使用两个字符串,字符串sb记录当前数字串及长度,strTemp记录下一个数字串及长度,如 果strTemp长度比sb长,则替换sb,即sb=strTemp,最后输出sb */ import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ StringBuilder sb = new StringBuilder(""); StringBuilder strTemp = new StringBuilder(); char[] ch = sc.nextLine().toCharArray(); for(int i=1;i<ch.length;i++){ if(ch[i]>='0' && ch[i]<='9'){ strTemp.append(ch[i]); } if(ch[i]<'0' || ch[i] >'9' || i == ch.length-1){ if(strTemp.length()>sb.length()){ sb = strTemp; } strTemp = new StringBuilder(""); } } System.out.println(sb); } } }
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine().trim()+"a";
char []strs = line.toCharArray();
int tmpl = 0;
int tmpi = 0;
for(int i = 0; i< strs.length; i++){
if(Character.isDigit(strs[i])){
for(int j =i+1; j< strs.length; j++){
if (!Character.isDigit(strs[j])) {
if(j-i>tmpl){
tmpl = j-i;
tmpi = i;
}
i = j;
break;
}
}
}
}
System.out.println(line.substring(tmpi, tmpi+tmpl));
}
}
用正则,空间换时间
import java.util.*;
public class NowCoder {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String first = in.nextLine();
String str[] = first.split("[^0-9]+");
int n =0;
int max = 0;
for (int i = 0; i < str.length; i++) {
if (str[i].length()>n){
n=str[i].length();
max=i;
}
}
System.out.println(str[max]);
}
}