题目描述:
参考这个图很容易找到规律:
根据题目中的欧几里得距离不能为2,得:( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) )不等于4;
若(x1-x2) * (x1-x2) = 0,且x1=x2的情况下,y1-y2 = 2;
在(y1-y2) * (y1-y2) = 0,且 y1=y2的情况下,x1-x2=2。
也就是说如果在[i][j]位置放了蛋糕,就不能在[i+2][j]和[i][j+2]位置放蛋糕了。
代码:
import java.util.Scanner;
//不要二
//本质就是四个蛋糕为一组 插空放
// xx xx xx
// xx xx xx
// xx xx xx
// xx xx xx
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int col=sc.nextInt();
int[][] array=new int[row][col];
//横向判断放蛋糕的位置
for (int i = 0; i <row; i++) {
if(i%4==0||i%4==1){
for(int j=0;j<col;j++){
if (j%4==0||j%4==1) {
array[i][j]=1;
}
}
}
//纵向判断放蛋糕位置
else{
for (int j=0;j<col;j++){
if(j%4==2||j%4==3){
array[i][j]=1;
}
}
}
}
//总数
int sum=0;
for (int i = 0; i <row; i++) {
for (int j=0;j<col;j++){
if(array[i][j]==1){
sum++;
}
}
}
System.out.println(sum);
}
}