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
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; }
相关文章推荐
- bnu1270 字符串位移 C语言版
- poj1270_toposort+回溯
- 还是动态规划。XDOJ Problem 1144
- POJ 1270 Following Orders 拓扑排序
- poj-1270 Following Orders(拓扑排序)
- XDOJ1013--时间工厂
- XDOJ1142 - 组合数学一之開司
- XDOJ1181 - 烤面包
- XDOJ1060 - 步行者
- CSU 1270: Swap Digits(数学啊 )
- XDOJ 猴子吃桃
- poj1270 拓扑序(DFS)
- hihoCoder #1270 : 建造基地 【dp】
- xdoj1000: a+b
- HDU 1270 小希的数表
- xdoj 1144 K叉哈弗曼树
- HiHoCoder #1270 : 建造基地 [Offer收割]编程练习赛1 【完全背包】
- 51nod- 1270 数组的最大代价
- xdoj 1142: 删除字符
- 51Nod 1270 数组的最大代价