Sum of Triangular Numbers
来源:互联网 发布:cda数据分析师报考 编辑:程序博客网 时间:2024/05/17 00:01
Description:Your task is to return the sum of Triangular Numbers up-to-and-including the nth Triangular Number.Triangular Number: "any of the series of numbers (1, 3, 6, 10, 15, etc.) obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, etc."[01]02 [03]04 05 [06]07 08 09 [10]11 12 13 14 [15]16 17 18 19 20 [21]e.g. If 4 is given: 1 + 3 + 6 + 10 = 20.Triangular Numbers cannot be negative so return 0 if a negative number is given.
题目如上
自己的的渣渣解法
int sumTriangularNumbers(int n){ return num(n , 1 , 0);}//n层数 m层次起始 count递归统计int num(int n , int m , int count){ int i = 0 ; if(count < n){ for(i = 0 ; i < count ; i++){ ++m; } return m + num(n , (m+1) , ++count); }else{ return 0; }}
别人优秀的解法
更好的规律 层次等于层次的累加
int sumTriangularNumbers(int n){ // triangular number int t = 0; // sum int s = 0; for (int i = 1; i < n+1; i++){ t = t + i; s = s + t; } return s;}
以及
这个不知道推导过程 = =
int sumTriangularNumbers(int n){ if(n<=0){ return 0; } return n*(n+1)*(n+2)/6;}
后记
在同学的推荐下,进入了codewars真的能学到东西,也能锻炼思维.
阅读全文
0 0
- Sum of Triangular Numbers
- Triangular numbers
- Sum of 'n' Numbers
- Sum of Numbers
- Sum of Square Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- 1120. Sum of Sequential Numbers
- sum of all integer numbers
- sum of all integer numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- The sum of prime numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- Ajax的应用
- React 可控组件input
- Ubuntu16.04 git连接远程库出现推送错误问题的解决方法
- Titanic: Machine Learning from Disaster初练习详解
- HDOJ2015 偶数求和
- Sum of Triangular Numbers
- Codeforces Round #349 (Div. 1)
- java中short类型自动转int类型注意事项
- 解决webstorm拉取Vue项目时卡顿,及内存爆满问题
- tensorflow API 之 tf.logging.set_verbosity
- HDU2255 奔小康赚大钱(二分图的最大完备匹配,KM算法)
- HDOJ2016 数据的交换输出
- 排序—选择排序
- 短链接服务的接实现 基于nginx url php redis 、js二维码生成 、js二维码识别