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

news/2024/9/22 14:34:57/

目录

一、题目

二、解答

(一)问题一:在记录完一组连续字符串后,没有注意判别紧随其后的非数字字符

(二)问题二:越界访问

(三)正确


一、题目

字符串中找出连续最长的数字串_牛客题霸_牛客网

二、解答

(一)问题一:在记录完一组连续字符串后,没有注意判别紧随其后的非数字字符

 

#include <iostream>
#include <cstdbool>
#include <string>
using namespace std;#include <iostream>
#include <cstdbool>
#include <string>
using namespace std;int main() {string s;cin >> s;int prev = 0, next = 0;string s2;bool isfirst = false;while (next < s.length()) {if (s[next] <= '9' && s[next] >= '0') {if (isfirst == false) {prev = next;isfirst = true;}++next;} else {isfirst = false;if (next - prev > s2.length()) {s2 = s.substr(prev, next - prev);}++next;}}if (next - prev > s2.length()) {s2 = s.substr(prev, next - prev);}cout << s2;return 0;}

(二)问题二:越界访问

#include <iostream>
#include <cstdbool>
#include <string>
using namespace std;int main() {string s;cin >> s;int prev = 0, next = 0;string s2;bool isfirst = false;while (next < s.length()) {if (s[next] <= '9' && s[next] >= '0'){if (isfirst == false){prev = next;isfirst = true;}++next;}else{isfirst = false;if (s[next - 1] <= '9' && s[next - 1] >= '0'){if (next - prev > s2.length()){s2 = s.substr(prev, next - prev);}}++next;}}if (s[next - 1] <= '9' && s[next - 1] >= '0'){if (next - prev > s2.length()) //注意最后一个数字串{s2 = s.substr(prev, next - prev);}}cout << s2;return 0;}

(三)正确

#include <iostream>
#include <cstdbool>
#include <string>
using namespace std;int main() {string s;cin >> s;int prev = 0, next = 0;string s2;bool isfirst = false;//判断prev是不是第一次出现while (next < s.length()) {if (s[next] <= '9' && s[next] >= '0'){if (isfirst == false){prev = next;isfirst = true;}++next;}else{isfirst = false;if (s[prev] <= '9' && s[prev] >= '0' && s[next - 1] <= '9' && s[next - 1] >= '0'){if (next - prev > s2.length()){s2 = s.substr(prev, next - prev);}}++next;}}if (s[next - 1] <= '9' && s[next - 1] >= '0')//注意跳出循环后的最后一个数字串{if (next - prev > s2.length()) {s2 = s.substr(prev, next - prev);}}cout << s2;return 0;}

 


http://www.ppmy.cn/news/1124580.html

相关文章

C#/Unity3D 单例模板(单例属性模板)

C# 单例单例属性 不做过多解释&#xff0c;非面向大众 using System; namespace EasyAVG {public static class SingletonProperty<T> where T : class{private static readonly object locker new object();private volatile static T instance null;public static…

nssm部署jar包

nssm部署jar包 1、软件下载 官方传送门 csdn下载地址 2、安装服务 F: cd F:\服务启动目录 set JAVA_HOMEF:\Program Files\Java\jdk1.8.0_181 set CLASSPATH.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOMe%\lib\tools.jar; set Path%JAVA_HOME%\bin; java -Dfile.encodingutf-8 -…

数据结构之【动态数组】

1. 线性表 概念&#xff1a;线性表是n个具有相同特性的数据元素的有限序列。 常见的线性表有&#xff1a;数组、链表、栈、队列、字符串…… 特点&#xff1a; 保存在这个结构中的元素都是相同的数据类型。元素之间线性排列&#xff0c;元素之间在逻辑上是连续的。 线性表…

德大黄鱼开捕 年产量20万吨 京东超市多举措保障黄鱼品质

作为“中国大黄鱼之都”&#xff0c;宁德大黄鱼占全国大黄鱼总产值产量均在90%以上。有关方面预计&#xff0c;今年全年宁德大黄鱼产量达20万吨&#xff0c;年增产10%&#xff0c;直接经济产值超百亿元。 宁德大黄鱼开捕活动&#xff0c;由宁德市政府、宁德渔业协会、京东超市…

Lua学习笔记:require非.lua拓展名的文件

前言 本篇在讲什么 Lua的require相关的内容 本篇需要什么 对Lua语法有简单认知 对C语法有简单认知 依赖Visual Studio工具 本篇的特色 具有全流程的图文教学 重实践&#xff0c;轻理论&#xff0c;快速上手 提供全流程的源码内容 ★提高阅读体验★ &#x1f449; ♠…

算法与数据结构-堆

文章目录 什么是堆如何实现一个堆&#xff1f;如何基于堆实现排序&#xff1f;1. 建堆2. 排序 什么是堆 堆是一种特殊的树&#xff0c;特殊点有二&#xff0c;如下&#xff1a; 堆是一个完全二叉树&#xff1b;堆中每一个节点的值都必须大于等于&#xff08;或小于等于&#…

SpringBoot 学习(九)Redis

11. 集成 Redis 11.1 说明 SpringBoot 操作数据&#xff1a;sping-data、jpa、jdbc、mongodb、redis SpringBoot 2. 后&#xff0c;jedis 被替换为 lettuce jedis&#xff1a;采用直连&#xff0c;多线程操作不安全&#xff0c;增强安全性需使用 jedis pool 连接池&#xff0…

如何编写测试用例,一篇搞定

前言 说到测试用例&#xff0c;但凡是软件测试从业人员&#xff0c;都不会陌生。但对于测试新手来说&#xff0c;测试用例仍旧有遗漏&#xff0c;或者写不好的时候。那么&#xff0c;究竟应该如何写好测试用例呢&#xff1f;今天就来针对性的聊聊这个话题。 在分析如何写测试…