XDOJ1270 - Rectangle Counting

来源:互联网 发布:网络监控系统示意图 编辑:程序博客网 时间:2024/06/06 09:39
Description

The length and the width of the following figure are 2. How many rectangles are there in this figure?

There are 9 rectangles in this figure. They are 4 rectangles with each side 1*1, 2 rectangles with each side 1*2, 2 rectangles with each side 2*1 and 1 rectangle with each side 2*2.

 

Input
The input consists of several test cases.
   The first line of the input contains a single integer T (0 < T ≤ 20), the number of test cases.
   Then Followed by T lines, each line gives a test case which contains two positive integers N and M, which means the length and the width of the figure( 1≤ N, M ≤ 10 ).
Output
  For each test case, output an integer indicating the number of rectangles in the figure.
Sample Input
2
1 1
2 2
Sample Output
1
9
解题 思路:

因为矩形的长和宽的选择没有相互关系,那么我们可以分别统计矩形的长和宽有多少种选择,然后由乘法原理得出矩形个数。以长的选择为例:网格长为N,则共有N+1个格点,于是长的选择方式为: C(N+1,2)=N*(N+1)/2 同理,宽的选择方式为: C(M+1,2)=M*(M+1)/2 矩形个数为N*(N+1)*M*(M+1)/4


#include<iostream>using namespace std;int main(){    int caseN;    cin>>caseN;    while(caseN--)    {        int N,M;        cin>>N>>M;        cout<<N*(N+1)*M*(M+1)/4<<endl;    }    return 0;}



0 0
原创粉丝点击