zoukankan      html  css  js  c++  java
  • 华为笔试题--输出连续最长数字串

    题目描述

    样例输出

    输出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;
        }
    }
  • 相关阅读:
    通过具名 slot (插槽)来显示Dialog 的标题
    elementUI 中,table表格如何实现当某一行被点击时会触发该事件(row-click)
    switch循环
    CSS动画
    for循环
    Display
    修改页面标题前的图标
    from表单
    CSS3文字效果
    CSS颜色渐变
  • 原文地址:https://www.cnblogs.com/joker1937/p/10661554.html
Copyright © 2011-2022 走看看