C - 递推

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


分析:本题的关键是找出规律,用公式表示出来,公式为s=n(n+1)/2*(m(m+1)/2),就是你分别按照行列计算矩形的个数(整体),然后相乘
AC代码:

#include <iostream>
using namespace std;
int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     int n,m,a,b,s;
     a=0;
     b=0;
     s=1;
     cin>>n>>m;
    for(;n>0;n--)
        a+=n;
    for(;m>0;m--)
        b+=m;
    s=a*b;
             cout<<s<<endl;
 }
    return 0;
}







posted @ 2016-08-04 19:56  踮起脚望天  阅读(170)  评论(0编辑  收藏  举报