c++语言实现的具体代码
时间: 2024-04-12 16:28:03 浏览: 103
以下是使用C++语言实现的具体代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int findLongestOnes(string sequence) {
int start = 0;
int length = 0;
int maxLength = 0;
for (int i = 0; i < sequence.length(); i++) {
if (sequence[i] == '1') {
length++;
} else {
maxLength = max(maxLength, length);
start = i + 1;
length = 0;
}
}
maxLength = max(maxLength, length);
return maxLength;
}
int main() {
string sequence;
cout << "请输入长度为n的01串:";
cin >> sequence;
int longestOnes = findLongestOnes(sequence);
cout << "最长连续1的区间长度为:" << longestOnes << endl;
return 0;
}
```
你可以将上述代码保存为一个.cpp文件,然后编译运行即可。在运行时,程序会要求你输入一个长度为n的01串,然后输出最长连续1的区间长度。
阅读全文