温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

字符串中找出连续出现的最大数字字符串

发布时间:2020-07-26 02:00:12 来源:网络 阅读:463 作者:be_better_ 栏目:编程语言

字符串中找出连续出现的最大数字字符串

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<string>
int main()
{
    string s1;
    while (getline(cin, s1))
    {
        int newlen = 0;//统计数字字符的长度
        int max=0;//数字字符的最大长度
        auto start = s1.begin();
        auto finish = s1.begin();
        string s2;
        while (start != s1.end()&&finish!=s1.end())
        {
            if (*start >= '0'&&*start <= '9')
            {
                newlen = 0;
                finish = start;
                while (finish != s1.end() && *finish >= '0'&&*finish <= '9'&&finish != s1.end())
                    //算出从当前位置起连续数字的最大值
                {
                    finish++;
                    newlen++;
                }
                if (newlen > max)//如果比之前的最大值大则替换
                {
                    s2.clear();
                    while (start != finish)
                    {
                        s2.push_back(*start);
                        start++;
                    }
                    max = newlen;
                }
                else//没之前的大就让start继续往后走
                {
                    start++;
                }
            }
            else
            {
                start++;
            }
        }
        cout << s2 << endl;
        s1.clear();
        s2.clear();
    }
    system("pause");
    return 0;
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI