输入一个字符串,求该字符串中最长连续数字序列出现的起始位置及其长度。 输入格式: 输入一个字符串,可包含空格。 输出格式: 输出内容:最长数字序列起始位置和长度空格隔开。输出尾部不能有空格。输入空串,输出
时间: 2023-11-26 09:48:50 浏览: 66
```python
def find_longest_num_sequence(s):
if not s:
return "NULL"
start, end = 0, 0
max_len = 0
i = 0
while i < len(s):
if s[i].isdigit():
j = i
while j < len(s) and s[j].isdigit():
j += 1
if j - i > max_len:
max_len = j - i
start = i
end = j - 1
i = j
else:
i += 1
return f"{start} {max_len}"
# 示例
s = "abc123def456ghi789"
print(find_longest_num_sequence(s)) # 输出:3 3
```
相关问题
C语言输入一个字符串,求该字符串中最长连续数字序列出现的起始位置及其长度
以下是C语言求字符串中最长连续数字序列出现的起始位置及其长度的代码实现:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int len, max_len = 0, start, max_start = 0, count = 0;
printf("请输入一个字符串:");
fgets(str, 100, stdin);
len = strlen(str);
for (int i = 0; i < len; i++) {
if (isdigit(str[i])) {
count++;
if (count == 1) {
start = i;
}
} else {
if (count > max_len) {
max_len = count;
max_start = start;
}
count = 0;
}
}
if (count > max_len) {
max_len = count;
max_start = start;
}
printf("最长连续数字序列出现的起始位置为:%d,长度为:%d\n", max_start, max_len);
return 0;
}
```
用C++代码实现 输入一个字符串,求该字符串中最长连续数字序列出现的起始位置及其长度
以下是用 C++ 实现输入一个字符串,求该字符串中最长连续数字序列出现的起始位置及其长度的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s); // 输入字符串
int start = -1; // 最长连续数字序列的起始位置
int len = 0; // 最长连续数字序列的长度
int curStart = -1; // 当前连续数字序列的起始位置
int curLen = 0; // 当前连续数字序列的长度
for (int i = 0; i < s.length(); i++) {
if (isdigit(s[i])) { // 如果当前字符是数字
if (curStart == -1) { // 如果当前没有连续数字序列
curStart = i; // 更新当前连续数字序列的起始位置
}
curLen++; // 更新当前连续数字序列的长度
} else { // 如果当前字符不是数字
if (curLen > len) { // 如果当前连续数字序列比最长连续数字序列更长
start = curStart; // 更新最长连续数字序列的起始位置
len = curLen; // 更新最长连续数字序列的长度
}
curStart = -1; // 重置当前连续数字序列的起始位置
curLen = 0; // 重置当前连续数字序列的长度
}
}
if (curLen > len) { // 如果最后一个连续数字序列比最长连续数字序列更长
start = curStart; // 更新最长连续数字序列的起始位置
len = curLen; // 更新最长连续数字序列的长度
}
if (start == -1) { // 如果没有连续数字序列
cout << "No consecutive digit sequence found." << endl;
} else { // 如果有连续数字序列
cout << "The longest consecutive digit sequence starts at position " << start << " and has length " << len << "." << endl;
}
return 0;
}
```
输入一个字符串后,程序会输出形如 "The longest consecutive digit sequence starts at position x and has length y." 或者 "No consecutive digit sequence found." 的信息,其中 x 和 y 分别为最长连续数字序列的起始位置和长度。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)