Description
给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格.
Input
第一行输入一个t, 表示有t组数据,然后每行输入n,m,分别表示网格的高和宽 ( n < 100 , m < 100).
Output
每行输出网格中有多少个矩形.
Sample Input
2 1 2 2 4
Sample Output
3 30
分析:
此题要用到一个数学公式:m到n的排列=m-1到n的排列+m-1到n-1的排列。
或者找出规律:矩形的个数和他长宽的关系:个数=[n*(n+1)/2] *[m*(m-1)/2]= n*(n+1)*m*(m-1)/4。发现这些很快解决此题
因此代码如下:
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int t, m, n; 6 cin>>t; 7 while(t--) 8 { 9 cin>>m>>n; 10 int k; 11 k=m*(m+1)*n*(n+1)/4; 12 cout<<k<<endl; 13 } 14 return 0; 15 }