MGJ6. 方格走法
描述
有一个X*Y的网格,小团要在此网格上从左上角到右下角,只能走格点且只能向右或向下走。请设计一个算法,计算小团有多少种走法。给定两个正整数int x,int y,请返回小团的走法数目。输入描述
输入包括一行,空格隔开的两个正整数x和y,取值范围[1,10]。输出描述
输出一行,表示走法的数目示例1
输入:
3 2
输出:
10
C 解法, 执行用时: 1ms, 内存消耗: 348KB, 提交时间: 2020-07-07
#include <stdio.h> #include <stdlib.h> int func(int x,int y) { if(x==0||y==0) return 1; else return func(x-1,y)+func(x,y-1); } int main(void) { int x,y; scanf("%d %d",&x,&y); printf("%d",func(x,y)); }
C 解法, 执行用时: 2ms, 内存消耗: 280KB, 提交时间: 2021-08-09
// dfs + memorization #include <stdio.h> #include <stdlib.h> #include <string.h> int depth_first_search_with_memorization_algorithm(int* dp, const int m, const int n, int x, int y) { if (x == n || y == m) return 0; if (x == n - 1 && y == m - 1) return 1; if (dp[y * n + x] >= 0) return dp[y * n + x]; int count = 0; count += depth_first_search_with_memorization_algorithm(dp, m, n, x + 1, y); // right count += depth_first_search_with_memorization_algorithm(dp, m, n, x, y + 1); // down return dp[y * n + x] = count; } int main(const int argc, const char** const argv) { int m, n; fscanf(stdin, "%d %d", &m, &n); ++m, ++n; int dp[m][n]; memset(dp, -1, sizeof dp); fprintf(stdout, "%d\n", depth_first_search_with_memorization_algorithm(dp, m, n, 0, 0)); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2019-08-04
#include<stdio.h> long long jiecheng(int n); int C_fun(int a,int b); int main() { int x,y; scanf("%d%d",&x,&y); printf("%d",C_fun(x+y,x)); // printf("20:%lld,10:%d\n",jiecheng(20),jiecheng(10)); return 0; } long long jiecheng(int n) { if(n==0||n==1) return 1; else { return n*jiecheng(n-1); } } int C_fun(int a,int b) { return jiecheng(a)/jiecheng(a-b)/jiecheng(b); }
C 解法, 执行用时: 2ms, 内存消耗: 352KB, 提交时间: 2019-07-03
#include<stdio.h> long long a[] = {0,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,121645100408832000,2432902008176640000}; int main() { int x,y; scanf("%d %d", &x, &y); printf("%lld", a[x+y]/(a[x]*a[y])); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2020-05-20
#include<stdio.h> long long jiecheng(int n); int C_fun(int a,int b); int main() { int x,y; scanf("%d%d",&x,&y); printf("%d",C_fun(x+y,x)); // printf("20:%lld,10:%d\n",jiecheng(20),jiecheng(10)); return 0; } long long jiecheng(int n) { if(n==0||n==1) return 1; else { return n*jiecheng(n-1); } } int C_fun(int a,int b) { return jiecheng(a)/jiecheng(a-b)/jiecheng(b); }