请计算n*m的棋盘格子(n为横向的格子数,m为竖向的格子数)从棋盘左上角出发沿着边缘线从左上角走到右下角,总共有多少种走法,要求不能走回头路,即:只能往右和往下走,不能往左和往上走。
注:沿棋盘格之间的边缘线行走
数据范围:
list1 = input().strip().split() n = int(list1[0])#提取n 注意n是“横向”的格子数 m = int(list1[1])#提取m 注意m是“竖向”的格子数 #画图分析,到达某一个点的路线条数等于到达其相邻两个点的路线条数之和 #那我们依次求出到达每一个点的路线次数即可 #初始化一个二维列表,注意点:沿着棋盘格的边缘线走 #所以棋盘格的边缘线的相交点即为一个站点,也就是一个元素 #所以二维列表就要初始化为m+1行,n+1列,可画图观察 arr = [[0 for j in range(n+1)] for i in range(m+1)] #不能走回头路,只能向右或向下 #所以在第一行的任意一个站点都只能有一条路到达,一路向右走 for i in range(n+1): arr[0][i] = 1 #在第一列的任意一个站点也只能有一条路到达,一路向下走 for i in range(m+1): arr[i][0] = 1 #依次求出到达每一个点的路线次数 for x in range(1,m+1): for y in range(1,n+1): arr[x][y] = arr[x][y-1] + arr[x-1][y] #至此,到达每一个点的路线条数都计算出来了 #查询最后一个点的路线条数即可 #需要注意的是,最后一个点的坐标是arr[m][n] print(arr[m][n])
import sys def dp_func(m, n): dp = [[0]*(n+1)]*(m+1) for i in range(m+1): dp[i][0] = 1 for i in range(n+1): dp[0][i] = 1 for i in range(1, m+1): for j in range(1, n+1): dp[i][j] = dp[i][j-1] + dp[i-1][j] return dp[m][n] for line in sys.stdin: m, n = [int(i) for i in line.split()] # dp[m][n] = dp[m][n-1] + d[m-1][n] print(dp_func(m, n))
# 2020年11月15日09:46:14 def path_way(n, m): if n==0 or m==0: return 1 else: return path_way(n-1, m) + path_way(n, m-1) while True: try: # 实际测试用例是一行空格输入,而不是题目中的两行,比如2 1 n, m = map(int, input().split()) # 分两次读入是通不过的 # n = int(input()) # m = int(input()) print(path_way(n, m)) except: break
为什么这个不能AC?
n 和 m 这样写有啥问题吗?
while True:
try:
n = int(input())
m = int(input())
# n, m = map(int, input().split())
dp = [[1 for i in range(n + 1)] for j in range(m + 1)]
dp[0][0] = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
# print(dp)
print(dp[-1][-1])
# print(max(dp[-1]))
except:
break
while True: try: n, m = map(int, input().split()) (maxx, minn) = (n, m) if n >= m else (m, n) nume = deno = 1 for i in range(1, minn+1): nume *= (maxx+i) deno *= i print(nume//deno) except: break
# 对于m*n的格子,就是从m+n步中选出m步向上或n步向右 # 因此为C(m+n,m)=C(m+n,n)种 from math import factorial while True: try: n, m = map(int, input().split()) print(int(factorial(n + m)/(factorial(n)*factorial(m)))) except: break
有重复元素的排列问题(横向格子重复n次,纵向格子重复m次),计算公式为:
def perm(n): dot = 1 for i in range(1,n+1): dot = dot * i return dot while True: try: [n,m] = list(map(int,input().split())) k = perm(m+n)/(perm(m)*perm(n)) print(int(k)) except: break
import sys def f(n,m): if min(n,m)==1: return 1 + max(n,m) else: return f(n,m-1) + f(n-1,m) for s in sys.stdin: s = s.strip().split(" ") r = f(int(s[0]), int(s[1])) print(r)
def rec(m,n): dp = [[0 for i in range(n+1)]]*(m+1) for i in range(m+1): dp[i][0] = 1 for i in range(n+1): dp[0][i] = 1 for i in range(1,m+1): for j in range(1,n+1): dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[m][n] while True: try: a,b = map(int,input().split()) print(rec(b,a)) except: break
import sys while True: line = sys.stdin.readline().strip() if not line: break n, m = int(line.split(' ')[0]), int(line.split(' ')[1]) if n > m: n, m = m, n res = 1 for i in range(n): res = res * (n + m - i) for j in range(n): res = res / (j + 1) print res
def chessboard(n, m): dp = [[1 for i in range(m+1)] for j in range(n+1)] for i in range(1, n+1): for j in range(1, m+1): dp[i][j] = dp[i][j - 1] + dp[i - 1][j] return dp[-1][-1] while True: try: n, m = map(int, input().split()) print(chessboard(n, m)) except: break