递归方法如下:
int f(int m, int n)
{
if (1 == m)
{
return n;
}
else if (1 == n)
{
return m;
}
return f(m, n - 1) + f(m - 1, n);
}
非递归方法如下:
int f(int m, int n)
{
int a[100][100];
for (int i = 0; i < m; ++i)
{
a[i][0] = i + 1;
}
for (int i = 0; i < n; ++i)
{
a[0][i] = i + 1;
}
for (int i = 1; i < m; ++i)
{
for (int j = 1; j < n; ++j)
{
a[i][j] = a[i - 1][j] + a[i][j - 1];
}
}
return a[m-1][n-1];
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-06 00:10:42