您的位置:首页 > 其它

XDOJ1270 - Rectangle Counting

2014-08-30 21:23 302 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: