码迷,mamicode.com
首页 > 其他好文 > 详细

[华为]在字符串中找出连续最长的数字串

时间:2017-04-06 01:23:17      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:输出   har   coder   div   pac   log   返回   stream   数字串   

链接:https://www.nowcoder.com/questionTerminal/2c81f88ecd5a4cc395b5308a99afbbec
来源:牛客网

样例输出

输出123058789,函数返回值9

输出54761,函数返回值5

 

接口说明

函数原型:

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

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

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

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

 

 

 

 

 

输入描述:

输入一个字符串。


输出描述:

输出字符串中最长的数字字符串和它的长度。如果有相同长度的串,则要一块儿输出,但是长度还是一串的长度

 

输入例子:
abcd12345ed125ss123058789

 

输出例子:
123058789,9

#include <iostream>
#include <string>

using namespace std;
int main()
{       
    string str;    
    while( cin>>str )    
    {        
        int i;        
        int max = 0;       
        string ss;        
        string out;        
        
        for(i = 0; i < str.size(); i++)        
        {           
            if(str[i] >= ‘0‘ &&str[i] <= ‘9‘)            
            {                
                ss += str[i];                
                while(str[i+1] >= ‘0‘ &&str[i+1] <= ‘9‘)                
                {                    
                    i++;                   
                    ss += str[i];               
                }                
                
                if(ss.size() > max)                
                {                   
                    max = ss.size();                   
                    out = ss;                                  
                }                
                
                else if(ss.size() == max)                   
                    out += ss;            
            }             
            ss.clear();                   
        }       
        cout<<out<<‘,‘<<max<<endl;           
    }    
    return 0;
}

  








[华为]在字符串中找出连续最长的数字串

标签:输出   har   coder   div   pac   log   返回   stream   数字串   

原文地址:http://www.cnblogs.com/hellochennan/p/6671197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!