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

描述

样例输出

输出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+"
");
    	
    }
}

  

原文地址:https://www.cnblogs.com/lxk2010012997/p/5402147.html