• 最大连续递增数字串


    题目:求最大连续递增数字串(如“ads3sl456789DF3456ld345AA”中的“456789”。

    答:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    //求最大连续递增数字串
    string FindMaxIncreNumberSeq(string str)
    {
        if ("" == str || str.length() <= 0)
        {
            return NULL;
        }
        int maxlength = 0;
        int start = -1;
        int count = 0;
        int pos = -1;
        bool isbegin = true;
        for (int i = 0; i < str.length(); i++)
        {
            if (str[i] >= '0' && str[i] <= '9')
            {
                if (isbegin)
                {
                    pos = i;
                    isbegin = false;
                    count = 1;
                }
                else if (str[i] - str[i - 1] == 1)
                {
                    count++;
                }
                else
                {
                    if (maxlength < count)
                    {
                        maxlength = count;
                        start = pos;
                    }
                    pos = i;
                    count = 1;
                }
    
                if (maxlength < count)
                {
                    maxlength = count;
                    start = pos;
                }
            }
            else
            {
                if (maxlength < count)
                {
                    maxlength = count;
                    start = pos;
                }
                isbegin = true;
            }
        }
        return str.substr(start, maxlength);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        string str = "23456789ads3sl456789DF012341234567893456ld345AA345678";
        cout<<"字符串:"<<str<<endl;
        cout<<"最大连续递增数字串:";
        cout<<FindMaxIncreNumberSeq(str);
    
        cout<<endl;
        return 0;
    }

    运行界面如下:

  • 相关阅读:
    3 path核心模块
    2 http
    运输层:TCP 连接管理
    运输层:TCP 流量控制
    运输层:TCP 可靠数据传输
    计算及网路:性能指标
    MySQL 并发控制概述
    运输层:流水线可靠数据传输协议
    运输层:可靠数据传输原理(FSM 描述)
    计算机网络概述
  • 原文地址:https://www.cnblogs.com/venow/p/2673088.html
Copyright © 2020-2023  润新知