大意为:
警察抓小偷。X为小偷,#为行人,数字代表警察可抓取的范围。
输入实例
9
X1X#2X#XX
注意边界条件,和字符型转换位数字 。
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int num = sc.nextInt();
String string = sc.next();
char[] str = string.toCharArray();
int count = 0;
System.out.println(Integer.valueOf(str[4])-48);
for (int i = 0; i < num; i++){
if (Character.isDigit(str[i])){
int digit = Integer.valueOf(str[i])-48;
//System.out.println(i+" "+digit);
if ((digit >= i) &&(digit+i < num)){
//左边界超出,右边界不超
for (int j = 0; j <= digit+i; j++){
if (str[j] == 'X'){
str[j] = '#';
count++;
System.out.println("执行1");
}
}
}else if((digit+i >= num) && (digit < i)){
//左边界不超出,右边界超出
for (int j = i-digit; j < num; j++){
if (str[j] == 'X'){
str[j] = '#';
count++;
System.out.println("执行2");
}
}
}else if((digit < i) &&(digit+i >= num)){
//左边界不超出,右边界不超出
for (int j = i-digit; j < num; j++){
if (str[j] == 'X'){
str[j] = '#';
count++;
System.out.println("执行3");
}
}
}else{
//左边界、右边界超出
for (int j = 0; j < num; j++){
if (str[j] == 'X'){
str[j] = '#';
count++;
System.out.println("执行4");
}
}
}
}
}
System.out.println(count);
}
}
}