Walking on the Grid
Problem Description Biving lives in Grid Kingdom, which is a special country as all its cities lie in a grid of size W*H.
Biving’s home locates in grid(1, 1) and she wants to go to grid(W, H) as soon as possible. In each step, she can walk from grid(I, J) to grid(I+1, J) or grid(I, J+1), but she can never walk out of the grid.
Here comes the question, how many path Biving can choose to achieve her goal. Two path Pi and Pj are treat as different if there exist some step Pi going to grid(x, y) but Pj don’t.
Input
The input contains multiple test cases (<= 100).
The first line of each test case contains two integer W, H(1<=W<=30, 1<=H<=30).
Output
For each case, output the path’s number modulo 1,000,000,007 in a single line.
Sample Input
1 1
2 2
3 3
Sample Output
1
2
6
怎么控制不让他超出范围?
#include<iostream>
using namespace std;
int main()
{
int W,H;
while(cin>>W>>H)
{
int sum1=1,sum2=1;
for(int i=1;i<W-1;i++)
{
sum1*=i;
}
for(int j=W+H-2;j>=H;j--)
{
sum2*=j;
}
cout<<sum2/sum1<<endl;
}
return 0;
}