博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Project Euler Problem 15 Lattice paths
阅读量:6983 次
发布时间:2019-06-27

本文共 1375 字,大约阅读时间需要 4 分钟。

Problem 15

Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20×20 grid?

C++:

#include 
#include
using namespace std;const int MAXN = 20;long grid[MAXN+1][MAXN+1];long pathcount(int row, int col){ long count; if(grid[row][col]) return grid[row][col]; if(row == 0 && col == 0) return 1; else if(row == 0) count = pathcount(row, col - 1); else if(col == 0) count = pathcount(row - 1, col); else count = pathcount(row - 1, col) + pathcount(row, col -1); grid[row][col] = count; return count;}int main(){ int n; memset(grid, 0, sizeof(grid)); while(cin >> n && n <=MAXN) { long ans = pathcount(n, n); cout << ans << endl; } return 0;}
C++:
#include 
using namespace std;const int MAXN = 20;long grid[MAXN+1][MAXN+1];int main(){ int n; while(cin >> n && n <=MAXN) { for(int j=0; j<=n; j++) grid[0][j] = 1; for(int i=1; i<=n; i++) { grid[i][0] = 1; for(int j=1; j<=n; j++) grid[i][j] = grid[i][j-1] + grid[i-1][j]; } cout << grid[n][n] << endl; } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7564019.html

你可能感兴趣的文章
行高计算
查看>>
oracle 中的闪回
查看>>
一键压缩脚本
查看>>
源码编译安装mysql
查看>>
甜、酸、苦、辣、咸与健康
查看>>
初识WEBGL
查看>>
BZOJ 1460 Pku2114 Boatherds
查看>>
适配器在JavaScript中的体现
查看>>
同一法
查看>>
snmp
查看>>
maya 专家模式
查看>>
[转]Shared——回调函数是什么
查看>>
本人对于netty框架的一些理解,怎么与网站上的websock建立连接
查看>>
SSM前后端分离及跨域
查看>>
json 和 table控件
查看>>
RabbitMQ基础概念详细介绍
查看>>
【es6】数组排重
查看>>
thinkphp字符截取函数msubstr()
查看>>
(42) Aeroo 模板实战
查看>>
NetSetMan IP地址切换工具
查看>>