牛客网-字符串中找出连续最长的数字串(好未来)

题目描述

读入一个字符串str,输出字符串str中的连续最长的数字串

输入描述:

个测试输入包含1个测试用例,一个字符串str,长度不超过255。

输出描述:

在一行内输出str中里连续最长的数字串。
示例1

输入

abcd12345ed125ss123456789

输出

123456789
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    string s;
    int max=0;
    int count=0;
    int index=0;
    while(cin>>s){
        for(int i=0;i<s.size();++i){
            if(s[i]>='0'&&s[i]<='9'){
                count++;
                if(max<count){
                    max=count;
                    index=i;
                }
            }
            else{
                count=0;
            }
        }
        for(int i=index-max+1;i<=index;++i){
            cout<<s[i];
        }
        cout<<endl;
    }
    //system("pause");
    return 0;
    

}

 

posted @ 2017-09-02 15:36  静悟生慧慧  阅读(508)  评论(0编辑  收藏  举报