题目描述:
在一个大型体育场内举办了一场大型活动,由于疫情防控的需要,要求每位观众必须间隔至少一个空位才允许落座。现在给出一排观众座位分布图,座位中存在已落座的观众,请计算出,在不移动现有观众座位的情况下,最多还能坐下多少名观众。
输入描述:
一个数组,用来标识某一排座位中,每个座位是否已经坐人。0表示该座位没有坐人,1表示该座位已经坐人,1 ≤ 数组长度 ≤ 10000
输出描述:
整数,在不移动现有观众座位的情况下,最多还能坐下多少名观众。
示例1:
输入
10001
输出
1
示例2:
输入
0101
输出
0
C++源码:
#include <iostream>
#include <string>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
while (true) {
getline(cin, str);
if (str == "\n") break;
if (str.size() <= 2) {
if (str.find('1') != string::npos) {
cout << 0 << endl;
break;
}
else {
cout << 1 << endl;
break;
}
}
int count0 = 1, countN = 0;
for (char s : str) {
if (s == '0') {
count0 += 1;
if (count0 == 3) {
countN += 1;
count0 = 1;
}
}
else {
count0 = 0;
}
}
if (str.back() != '1' && str.find('1') != string::npos) {
size_t pos = str.rfind('1');
string temp = str.substr(pos + 1);
if (temp.size() % 2 == 0) {
countN += 1;
}
}
cout << countN << endl;
}
system("pause"); // 暂停程序,等待按键
return 0;
}