设有一个n*m方格的棋盘(1≤m,n≤100)。
求出该棋盘中包含多少个正方形、多少个长方形(不包括正方形)。
例如:
当n=2,m=3时
这道题我的思路是:
外面两层采用坐标,里面两层这是定长后遍历宽度
import java.util.Scanner;
public class Java01
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//长
int m = sc.nextInt();//宽
int ans = 0;
int res = 0;
int x=0,y=0;
for(;x<n;x++){
for(y=0;y<m;y++){
for (int i = 1; i<=n; i++) {
for (int j = 1;j<=m; j++) {
if(x+i<=n && y+j<=m){
if (i==j)ans++;
else res++;
}
}
}
}
}
System.out.println(ans+" "+res);
}
}