您的位置:首页 > 其它

在字符串中找出连续最长的数字串

2016-04-17 20:55 239 查看
描述样例输出

输出123058789,函数返回值9

输出54761,函数返回值5

接口说明

函数原型:

unsignedint Continumax(char** pOutputstr, char* intputstr)

输入参数:
char* intputstr 输入字符串;

输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;

返回值:
连续最长的数字串的长度

知识点位运算
运行时间限制10M
内存限制128
输入输入一个字符串。

输出输出字符串中最长的数字字符串和它的长度。

如果数字字符串为空,则只输出0

如 input: dadfsaf output:0

样例输入abcd12345ed125ss123058789
样例输出123058789,9
package com.oj5;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int[] length = new int[str.length()];
int[] begin = new int[str.length()];
int pos = 0;

int count = 0;
boolean beginflag = false;
for(int i = 0;i < str.length(); i++)
if(str.charAt(i)<='9'&&str.charAt(i)>='0'&&beginflag==false){
beginflag = true;
count++;
begin[pos] = i;
}else if(str.charAt(i)<='9'&&str.charAt(i)>='0'){
count++;
}else if(beginflag==true&&(str.charAt(i)<'0'||str.charAt(i)>'9')){
length[pos++] = count;
count = 0;
beginflag = false;
}
if(beginflag==true)
length[pos++] = count;

int max = 0;
for(int i = 0;i < pos; i++)
if(length[i]>max)
max = length[i];
if(max==0){
System.out.println(0);
return ;
}

for(int i = 0;i < pos; i++)
if(length[i]==max){
for(int j = begin[i];j < begin[i]+length[i]; j++)
System.out.print(str.charAt(j));
System.out.print(",");
}
System.out.print(max+"\n");

}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: