题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
// HJ92-2 在字符串中找出连续最长的数字串.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
while (cin >> s)
{
vector<string>vec;
int count=0,max=0;
for (int i = 0; i < s.size(); i++)
{
count = 0;
if (isdigit(s[i]))
{
int j = i;
while (isdigit(s[j]))
{
count++;
j++;
}
if (max < count)
{
vec.clear();
max = count;
vec.push_back(s.substr(i, count));
}
else if (max == count)
{
vec.push_back(s.substr(i, count));
}
i = j;
}
}
for (auto c : vec)
{
cout << c;
}
cout << "," << max << endl;
}
return 0;
}

查看24道真题和解析