Sum of numbers from 0 to N -- 7 kyu
来源:互联网 发布:python 量化投资书籍 编辑:程序博客网 时间:2024/05/20 00:35
原题
https://www.codewars.com/kata/56e9e4f516bcaa8d4f001763/train/cpp
题目
Description:
We want to generate a function that computes the series starting from 0 and ending until the given number following the sequence:
0 1 3 6 10 15 21 28 36 45 55 ….
which is created by
0, 0+1, 0+1+2, 0+1+2+3, 0+1+2+3+4, 0+1+2+3+4+5, 0+1+2+3+4+5+6, 0+1+2+3+4+5+6+7 etc..
Example:
Input:6
Output : 0+1+2+3+4+5+6 = 21Input : -15
Output : -15<0Input : 0
Output : 0=0
分析
- 输入数值当大于0时,输出从0到输入数值的累加的字符串;
- 输入数值当小于0时,输出该数<0;
- 输入数值为0时,输出0=0;
代码
using namespace std;class SequenceSum{ int count; public: SequenceSum (int); string showSequence();};string SequenceSum::showSequence(){ string res; int sum(0); stringstream s_sum; if(count < 0){ s_sum<<count; res = string(s_sum.str())+string("<0"); return res; } if(count == 0){ res= string("0=0"); return res; } for(size_t i = 0; i <= count ;i++){ stringstream s_plus; s_plus<<i; cout << s_plus.str() <<endl; sum +=i; res += string(s_plus.str())+string("+"); } res.pop_back(); s_sum<<sum; res += string(" = ") + string(s_sum.str()); return res;}SequenceSum::SequenceSum (int c) { count = c;}
参考代码
using namespace std; class SequenceSum{ int count; public: SequenceSum (int); string showSequence(); }; string SequenceSum::showSequence(){ if(0 == count) return "0=0"; if(0 > count) return to_string(count) + "<0"; ostringstream oss; for(size_t i=0;i<=count;i++){ oss << i; if(i!=count) oss <<"+"; else oss << " = " << (count*(count+1)>>1); } return oss.str(); } SequenceSum::SequenceSum (int c) { count = c; }// 单独数字转字符串可使用to_string() (C++11)// 多个数字转字符串可使用ostringstream// 多个数字有序数列求和,可用高斯求和公式n*(n+1)/2//作者:jdzhangxin//链接:http://www.jianshu.com/p/5bde637c3bd9//來源:简书//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
阅读全文
0 0
- Sum of numbers from 0 to N -- 7 kyu
- Sum of 'n' Numbers
- Do you know how many combinations to select n numbers from 1 to m with sum of which is mysum?
- Number of sets of natural numbers less than n which sum to n.
- Palindrome Number & Remove Nth Node From End of List & Sum Root to Leaf Numbers
- Select n numbers from 1 to m with all of array 'all' and none of array 'none'
- Parts of a list -- 7 kyu
- Find n'th Digit of a Number -- 8 kyu
- perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
- 1960. Sum of 1 to n
- Sum of Numbers
- Sum of Square Numbers
- Sum of Triangular Numbers
- 5.4.7 Sum Root to Leaf Numbers
- 5.4.7 Sum Root to Leaf Numbers
- To find sum of two numbers without using any operator. Only use of printf() is allowed.
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- LCD的配置
- 现代OpenGL+Qt学习笔记之二:程序框架
- 静态代码块、构造代码块与构造函数的调用顺序
- 判断素数
- JavaScript面向对象(二)——成员属性、静态属性、原型属性与JS原型链
- Sum of numbers from 0 to N -- 7 kyu
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛E Half-consecutive Numbers
- java提高篇(九)-----详解匿名内部类
- !互联网进入付费时代,准备好了吗?
- 字段类型char(n)、varchar(n)区别
- 【题意理解】The New President UVALive
- 写一段JDBC连接的程序,并实现数据查询。
- LeetCode371. Sum of Two Integers
- 详解匿名内部类