赞
踩
m行n列的网格,从左上角(1,1)出发,每一步只能向下或者向右,问共有多少种方法可以走到右下角(m,n);
输入参数 m n (1<=m<=10 1<=n<=10)
输出多少种走法
比如:
输入:2 3
输出:3
输入:5 5
输出:70
完整代码(C++):
#include<iostream> using namespace std; int sum(int m, int n) { int total; if (m == 1 || n == 1) { return 1; } if (m == 2 && n == 2) { return 2; } return sum(m - 1, n) + sum(m, n - 1); } int main() { int m, n; cin >> m; cin >> n; int total; total = sum(m, n); cout << total << endl; return 0; }
结果:
注意:最后调用的时候,是调用sum(m,n),而不是sum(m+1,n+1).
参考博客:
机器人走网格
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。