题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include<iostream>
#include<vector>
using namespace std;
int main()
{
string a;
while(cin>>a)
{
vector<string>b;
for(int i=0;i<a.length();i++)
{
if(a[i]>='0'&&a[i]<='9')
{
string c;
c+=a[i];
for(int j=1;j<a.length()-i;j++)
{
if(a[i+j]>='0'&&a[i+j]<='9'&&i+j!=a.length()-1)
{
c=c+a[i+j];
}
else if(a[i+j]<'0'||a[i+j]>'9')
{
b.push_back(c);
i=i+j;
break;
}
else if((a[i+j]>='0'&&a[i+j]<='9')&&i+j==a.length()-1)
{
c=c+a[i+j];
b.push_back(c);
break;
}
}
}
}
int d=0;
for(auto x:b)
{
if(x.length()>d)
{
d=x.length();
}
}
for(auto x:b)
{
if(x.length()==d)
{
cout<<x;
}
}
cout<<','<<d<<endl;
}
return(0);
}
#include<vector>
using namespace std;
int main()
{
string a;
while(cin>>a)
{
vector<string>b;
for(int i=0;i<a.length();i++)
{
if(a[i]>='0'&&a[i]<='9')
{
string c;
c+=a[i];
for(int j=1;j<a.length()-i;j++)
{
if(a[i+j]>='0'&&a[i+j]<='9'&&i+j!=a.length()-1)
{
c=c+a[i+j];
}
else if(a[i+j]<'0'||a[i+j]>'9')
{
b.push_back(c);
i=i+j;
break;
}
else if((a[i+j]>='0'&&a[i+j]<='9')&&i+j==a.length()-1)
{
c=c+a[i+j];
b.push_back(c);
break;
}
}
}
}
int d=0;
for(auto x:b)
{
if(x.length()>d)
{
d=x.length();
}
}
for(auto x:b)
{
if(x.length()==d)
{
cout<<x;
}
}
cout<<','<<d<<endl;
}
return(0);
}