题解 | #在字符串中找出连续最长的数字串#

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

https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec

#include<iostream>
using namespace std;


int main()
{
    string s;
    
    while(cin>>s){
        int len=s.length();
        string res="";
        string t="";
        int i=0;
        int cnt=0;
        int ans=0;
        while(i<len){
            cnt=0;
            if(!isdigit(s[i])){
                i++;
                t="";
            }else{
                while(isdigit(s[i])&&i<len){
                    t+=s[i++];
                    cnt++;
                }
                if(cnt>ans){
                    res=t;
                    ans=cnt;
                    cnt=0;
                    t="";
                }else if(cnt==ans){
                    res+=t;
                    t="";
                }
            } 
        }
        cout<<res<<","<<ans<<endl;
    }

    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 15:19
简历上能写3个月吗?
码农索隆:大胆写,主要你能把实习经历包装好,可以看一下我这篇帖子https://www.nowcoder.com/share/jump/4888395581180798063
点赞 评论 收藏
分享
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
06-23 11:43
门头沟学院 Java
allin校招的烤冷面很爱看电影:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务