杭电ACM----------1001 Sum problem

来源:互联网 发布:mac优盘装win7 编辑:程序博客网 时间:2024/06/05 04:42
Problem DescriptionHey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. InputThe input will consist of a series of integers n, one integer per line. OutputFor each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer. Sample Input1100 Sample Output15050


要解决这样的问题,首先想到的就是递归

int SumN(int n){if(n == 1){return n;}else{return n + SumN(n - 1);}}





0 0
原创粉丝点击