EP3. 嵌入式牛牛疑惑的变量
描述
示例1
输入:
10
输出:
55
C 解法, 执行用时: 3ms, 内存消耗: 304KB, 提交时间: 2022-08-06
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ int recursion(int n ) { // write code here static int m=0; for(;n>0;n--){ m=m+n; } return m; }
C 解法, 执行用时: 3ms, 内存消耗: 304KB, 提交时间: 2022-08-05
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ int recursion(int n ) { static int m = 0; if (n != 0) { m += n; return recursion(n - 1); } return m; }
C 解法, 执行用时: 3ms, 内存消耗: 304KB, 提交时间: 2022-08-04
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ #define N_NOT_ZERO(n) { \ if(n != 0){ \ m += n; \ recursion(--n); \ } \ }; int recursion(int n ) { // write code here static int m = 0; N_NOT_ZERO(n); return m; }
C 解法, 执行用时: 3ms, 内存消耗: 308KB, 提交时间: 2022-08-04
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ int recursion(int n ) { // write code here static int m = 0; if(n == 0) return m; recursion(n-1); m += n; return m; }
C 解法, 执行用时: 3ms, 内存消耗: 312KB, 提交时间: 2022-08-05
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ int recursion(int n ) { // write code here static int m = 0; if(n == 0) return m; recursion(n-1); m += n; return m; }