华为笔试题--输出连续最长数字串

题目描述

样例输出

输出123058789,函数返回值9

输出54761,函数返回值5

接口说明

函数原型:

   unsignedint Continumax(char** pOutputstr,  char* intputstr)

输入参数:
   char* intputstr  输入字符串;

输出参数:
   char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;  

返回值:
  连续最长的数字串的长度

示例1

输入

abcd12345ed125ss123058789

输出

123058789,9

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    string str;
    int tmp = 0;
    int max = 0;
    while (cin >>str) {
        tmp = 0;
        max = 0;
        vector<int> pos;
        for(int i = 0; i < str.size(); ++i) {
            if(str[i] >= '0' && str[i] <= '9') tmp++;
            else tmp = 0;
            if(tmp == max) pos.push_back(i); 
            else if(tmp > max) {
                max = tmp;
                pos.clear();
                pos.push_back(i);
            } 
        }
        for(int j = 0; j < pos.size(); ++j) {
            cout << str.substr(pos[j]-max+1, max);
        }
        cout << ',' << max << endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/joker1937/p/10661554.html