遍历路灯字符串,遇见“.”,就给计数器+1,然后往后挪三个位置。如果遇到“X”,就直接往后挪一个位置。
if __name__ == '__main__':
count = int(input()) # 测试用例的个数
n = []
lantern = []
for i in range(count):
n_tmp = int(input()) # 路灯个数
n.append(n_tmp)
lantern_tmp = input() # 路灯分布字符串
lantern.append(lantern_tmp)
lantern_count = [0 for i in range(count)] # 存储最终结果的数组
for i in range(len(lantern)): # 循环路灯数
j = 0
while (j < len(lantern[i])): # 循环对应路灯排列字符串
if lantern[i][j] == '.':
j += 3
lantern_count[i] += 1
else:
j += 1
print(lantern_count[0])
for i in range(len(lantern_count) - 1):
print(lantern_count[i + 1])
链接:https://www.nowcoder.com/questionTerminal/3a3577b9d3294fb7845b96a9cd2e099c?orderByHotValue=1&mutiTagIds=149&page=1&onlyReference=false
来源:牛客网
#include
#define maxlen 1007
char road[maxlen];
int main() {
int t, i;
scanf("%d", &t);
for (i = 0; i < t; ++i) {
int len, count = 0;
scanf("%d", &len);
scanf("%s", road);
char *p;
for (p = road; p < road + len; ++p) {
if (*p == '.') {
p[1] = p[2] = 'X';
count++;
}
}
printf("%d\n", count);
}
return 0;