Question source
Niuke.com
Link: Find the longest continuous number string in the string
Title description
Read in a string str, output the longest continuous number string in the string str
Enter a description:
The test input contains 1 test case, a string str, and the length does not exceed 255.
Output description:
Output the longest continuous number string in str in one line.
enter:
abcd12345ed125ss123456789
Output:
123456789
The problem-solving idea is to
traverse the string and use cur to record the continuous number string. If it is not a number character, it means that a continuous number string is over, then the number string is compared with the previous number string, if it is longer, then update The longer number string is updated to res.
Code display
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str,res,cur;
cin >> str;
for(int i = 0;i <= str.length();i++)
{
if(str[i] >= '0' && str[i] <= '9')
{
cur += str[i];
}
else
{
if(res.size() < cur.size())
res = cur;
else
cur.clear();
}
}
cout << res;
return 0;
}