#include <stdio.h> #include <string.h> int main(){ int i,k,key,sum,max,len[300]; char a[300]; scanf("%s",a); for(int j=0;j<300;j++){ //初始化长度数组 len[j]=0; } for(i=0;i<strlen(a);i++){ k=i; sum=0; while(a[i]>='0' && a[i]<='9'){ i++; sum++; } len[k]=sum; //将每个位置出现的数组串长度,保存在len[]中,没出现的为 0 } max=0; for(i=0;i<strlen(a);i++){ if(max<len[i]){ max = len[i]; //保存最长的数字串长度 key = i; //记录最长数字串, 初始地址 } } for(i=0;i<max;i++){ //从key下标输出,max个数字 printf("%c",a[key++]); } printf("\0"); printf("\n"); return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String string=in.next(); System.out.println(findMaxDigits(string)); } private static String findMaxDigits(String string) { int tempMax=0; int max=0; String result=""; char[] ch=string.toCharArray(); StringBuilder sb=new StringBuilder(); for(int i=0;i<ch.length;i++){ if(Character.isDigit(ch[i])){ sb.append(ch[i]); tempMax++; }else{ tempMax=0; sb.delete(0, sb.length()); } if(max<tempMax){ max=tempMax; result=sb.toString(); } } return result; } }
let line = readline();let arr = line.charAt(0);
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)); } }
#include <bits/stdc++.h> using namespace std; int main() { string a; cin>>a; int j=0; int count=0; int max=0; int maxi; for(int i=0;a[i]!='\0';i++) { if((int)a[i]<58&&(int)a[i]>47) //判断是不是0-9的数字 { count++; if(count>max) { max=count; //记录最大长度 maxi=i; //记录最大长度的结尾坐标 } } else //不满足条件,重新计数 { count=0; } } for(int i=maxi-max+1;i<=maxi;i++) { cout<<a[i]; } }
/* 使用两个字符串,字符串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.util.ArrayList; import java.util.Scanner; public class Solution30 { public static String MaxSubArray(char[] chars, int n) { if (n == 0) { return ""; } ArrayList<Character> curSum = new ArrayList<>(); ArrayList<Character> maxSum = new ArrayList<>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { if (Character.isDigit(chars[i])) { curSum.add(chars[i]); } else { if (curSum.size() > maxSum.size()) { maxSum.clear(); for (int j = 0; j < curSum.size(); j++) { maxSum.add(curSum.get(j)); } } curSum.clear(); } } for (int i = 0; i < maxSum.size(); i++) { sb.append(maxSum.get(i)); } return sb.toString(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); char[] chars = str.toCharArray(); char[] newChars = new char[str.length() + 1]; for (int i = 0; i < chars.length; i++) { newChars[i] = chars[i]; } int n = newChars.length; newChars[n - 1] = '#'; System.out.println(MaxSubArray(newChars, n)); } }
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.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()){ String s = sc.nextLine(); char[] arr = s.toCharArray(); StringBuffer sb = new StringBuffer(); String result = ""; for (int i = 0; i < s.length() ; i++) { if(arr[i]>='0' && arr[i]<='9'){ sb.append(arr[i]); }else{ sb.setLength(0); } if(sb.length() > result.length()){ result = sb.toString(); } } System.out.println(result); } } }
//暴力破解,把数组里面所有的数字串和它的长度存下来,然后输出最长的一个。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct number_str
{
string math;
int length;
};
bool comp(number_str A,number_str B)
{
return A.length>B.length;
}
int main(int argc, const char * argv[]) {
string A;
cin>>A;
vector<number_str> numbers;
number_str temp;
for(int i=0;i<A.length();i++)
{
if(A[i]<='9'&&A[i]>='0')
{
for(int j=i+1;j<=A.length();j++)
{
if(A[j]>'9'||A[j]<'0'||j==(A.length()))
{
temp.math=A.substr(i,j-i);
temp.length=int(temp.math.length());
numbers.push_back(temp);
i=j;
break;
}
}
}
}
sort(numbers.begin(), numbers.end(), comp);
cout<<numbers[0].math<<endl;
return 0;
}
根据输入的string字符串初始化对应的int型数组,比如输入为abcd56789ww,则
对应的int数组为00001234500,找出int数组的最大值并记录位置,根据这个位置
和最大值输出最长数字串。
#include <iostream>
#include <string>
using namespace std;
int main() { string str; int A[255]; int i = 1; int locat = 0, max = 0; cin >> str; int N = str.size(); if (str[0] >= '0' && str[0] <= '9') A[0] = 1; else A[0] = 0; while (i < N) { if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) { A[i] = 0; i++; } if (str[i] >= '0' && str[i] <= '9') { A[i] = A[i-1] + 1; i++; } } for (int i = 0; i < N; i++) { if (A[i] > max) { max = A[i]; locat = i; } } for (int i = (locat - max + 1); i <= locat; i++) { cout << str[i]; } return 0;
}
#include <iostream> using namespace std; int main() { string s,t; int Max = 0, cnt = 0; cin>>s; for(int i=0;i<s.length();i++) { if(s[i]>='0' && s[i]<='9') { cnt++; if(Max < cnt) { Max = cnt; t = s.substr(i-Max+1, Max); } }else cnt = 0; } cout<<t<<endl; return 0; }
C++版本,O(n)时间复杂度
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
cin >> str;
int count = 0;
int maxnum = 0;
int index = 0;
int i = 0;
for (char c : str){
if (c >= '0'&&c <= '9'){
count++;
if (count > maxnum){
maxnum = count;
index = i;
}
}
else{
count = 0;
}
i++;
}
for (int j = maxnum-1; j>=0; j--)
{
cout << str[index - j];
}
}