思路:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根不等于2,那么( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) )不等于4,也就是(x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) 这两部分的结果之和不为4,因为x1,x2,y1,y2都为整数,那么这两部分之和不为4只能是0+4=4和4+0=4两种结果,也就是说x1=x2&&y1=y2+2或者x1=x2+2&&y1=y2两种情况。那么这道题思路已经很清晰了,就是让所有位置都为1,然后再从左上角开始遍历,设当前位置为(x,y),将(x+2,y)和(x,y+2)的位置的元素置为0。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int count = 0;//蛋糕数
int[][] arr = new int[a][b];
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = 1;
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (arr[i][j] == 1) {
count++;
if (i + 2 < a) {//判断是否越界
arr[i + 2][j] = 0;
}
if (j + 2 < b) {//判断是否越界
arr[i][j + 2] = 0;
}
}
}
}
System.out.println(count);
}
}

京公网安备 11010502036488号