华为od 找座位 C++
题目描述
在一个大型体育场内举办了一场大型活动,由于疫情防控的需要,要求每位观众的必须间隔至少一个空位才允许落座。
现在给出一排观众座位分布图,座位中存在已落座的观众,请计算出,在不移动现有观众座位的情况下,最多还能坐下多少名观众。
输入描述
一个数组,用来标识某一排座位中,每个座位是否已经坐人。0表示该座位没有坐人,1表示该座位已经坐人。
- 1 ≤ 数组长度 ≤ 20000
输出描述
整数,在不移动现有观众座位的情况下,最多还能坐下多少名观众。
用例1
输入
10001
输出
1
用例2
输入
0101
输出
0
C++
跟停车场车辆统计思路一样。
#include <iostream>
#include <string>
using namespace std;
int main() {
string seats;
cin >> seats;
// 找到字符串中的"000"并用"#"替换
int pos = seats.find("000");
while (pos != string::npos) {
seats.replace(pos, 3, "#");
pos = seats.find("000", pos + 1);
}
// 统计字符串中"#"的个数
int count = 0;
for (char seat : seats) {
if (seat == '#') {
count++;
}
}
// 输出统计结果
cout << count << endl;
return 0;
}