m*n 矩阵中求正方形个数

<?php
/**
 * Notes:
 * User: liubing17
 * DateTime: 2019-10-17 17:10
 */
function get($m, $n){
    /*
     * 获取m*n矩阵正方形的个数
     * */
    if($m*$n <=0 ){
        return 0;
    }
    $total = 0;
    while($m>0 && $n>0){
        $total += $m*$n;
        $m--;
        $n--;
    }
    return $total;
}
echo get(1,1).PHP_EOL;

    

m*n矩阵(宽为m单位长度,长为n单位长度)所构成的正方形个数=边长为1,2,3 ...min(m,n)时正方形个数的总和

  边长为1时:宽 有m种选法,高有n种选法,总共有m*n种选法

 边长为2时:宽有m-1种选法,高有n-1种选法,总共有(m-1)*(n-1)种选法

 ...

边长为x(x=min(m,n))时:宽有m-x种选法,高有n-x种选法,总共有(m-x)*(n-x)种选法

一次循环即可

猜你喜欢

转载自www.cnblogs.com/LiuBingBlogs/p/11694347.html
mn