题目描述
小Q正在给一条长度为n的道路设计路灯安置方案。
为了让问题更简单,小Q把道路视为n个方格,需要照亮的地方用’.'表示, 不需要照亮的障碍物格子用’X’表示。
小Q现在要在道路上设置一些路灯, 对于安置在pos位置的路灯, 这盏路灯可以照亮pos - 1, pos, pos + 1这三个位置。
小Q希望能安置尽量少的路灯照亮所有’.'区域, 希望你能帮他计算一下最少需要多少盏路灯。
注意java吸收空格啊!!!!
有点麻烦
思路就是每当遇到一个需要放路灯的点,将路灯放到当前点的前一格,简单的贪心思想。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = 1;
t = cin.nextInt();
cin.nextLine();
while(t>0)
{
int l;
l = cin.nextInt();
cin.nextLine();
String s = new String();
s = cin.nextLine();
int ans = 0;
int now = 0;
while(now<l)
{
if(s.charAt(now)=='X')
{
now++;
continue;
}
else
{
ans++;
now+=3;//相当于三格都被照亮
}
}
System.out.println(ans);
t--;
}
}
}
1209

被折叠的 条评论
为什么被折叠?



