如何用C语言来计算一个矩形中包含多少个小矩形

来源:百度知道 编辑:UC知道 时间:2024/05/29 19:30:24
for example
在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1*2的矩形,2个2*2的矩形,2个3*1的矩形和1个3*2的矩形,总共18个矩形。
给出矩阵的长宽A,B,计算可以从中找到多少个矩形。

1、在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1*2的矩形,2个2*2的矩形,2个3*1的矩形和1个3*2的矩形,总共18个矩形。给出矩阵的长宽A,B,计算可以从中找到多少个矩形。

这个题目的解决方法就是找到递推式,然后用迭代找到答案。

2、例程:

#include <stdio.h>
/*计算一个维度上容纳的边数*/
int GetSubBoundCount(int l,int subl)
{
    int count = l - subl + 1;
    return count > 0 ? count : 0;
}
/*计算可容纳指定尺寸的矩形数*/
int GetSubRectCount(int w,int h,int subw,int subh)
{
    return GetSubBoundCount(w,subw) * GetSubBoundCount(h,subh);
}
int main()
{
    int w,h;
    scanf("%d%d",&w,&h);
    int count = 0;
    int x,y;
    for(x