lipeng 发表于 2017-7-10 20:39:58

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

  链接: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 >= '0' &&str <= '9')            
{               
ss += str;               
while(str >= '0' &&str <= '9')               
{                  
i++;                  
ss += str;               
}               
if(ss.size() > max)               
{                  
max = ss.size();                  
out = ss;                                 
}               
else if(ss.size() == max)                  
out += ss;            
}            
ss.clear();                  
}      
cout<<out<<','<<max<<endl;         
}   
return 0;
}
页: [1]
查看完整版本: [华为]在字符串中找出连续最长的数字串