Java
解题思路:对于每个字符串从左向右遍历,遇到‘.’则将结果➕1,同时将该位置及其右一、右二,三个位置的字符值为‘X’。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); int[] arrNum = new int[t]; String[] arrStr = new String[t]; for(int i=0; i<t; i++) { arrNum[i] = scan.nextInt(); arrStr[i] = scan.next(); } scan.close(); // 依次处理每个字符串 for(int i=0; i<t; i++) { int len = arrNum[i]; String curStr = arrStr[i]; lightNum(len, curStr); } } private static void lightNum(int len, String str) { int res = 0; char[] charArr = str.toCharArray(); for(int i=0; i<len; i++) { if(charArr[i] == '.') { res++; charArr[i] = 'X'; if(i<len-1) { charArr[i+1] = 'X'; } if(i<len-2) { charArr[i+2] = 'X'; } } } System.out.println(res); } }