字符串中找连续最长的数字串

  • 题目:
    字符串中找连续最长的数字串_第1张图片

  • 示例
    字符串中找连续最长的数字串_第2张图片

  • 代码实现

#include
#include
using namespace std;
char * GetMaxCount(char *str,size_t size)
{
    if (size == 0)
        return NULL;
    int count = 0;
    int max = 0;
    size_t i = 0;
    char *cur = str;
    char *start = NULL;
    char *ret = start;
    size_t j = 0;

    while (i < size)
    {
        start = cur;
        while (i < size && *cur >= '0' && *cur <= '9')
        {
            count++;
            cur++;
            i++;
        }

        if (i <= size)
        {
            if (count > max)
            {
                max = count;
                j = i;
                ret = start;
                str[j] = '\0';
            }
            cur++;
            i++;
            count = 0;
        }

    }

    return ret;

}
int main()
{
    string str;
    getline(cin, str);
    char *ret = GetMaxCount((char*)str.c_str(),str.size());
    cout << ret;
    return 0;
} 

你可能感兴趣的:(牛客在线编程题)