12 求1+2+...+n

参考 http://www.cppblog.com/zengwei0771/archive/2012/04/28/173014.html 和 http://blog.csdn.net/shiren_bod/article/details/6703467

代码:

#include<iostream>
using namespace std;

long add_func(int n,long& sum)
{
    (n>0) && (add_func(n-1,sum));
    return (sum+=n);
}

int main(void)
{
    int n=100;
    long sum=0;
    
    cout<<"1+2+...+100="<<add_func(n,sum)<<endl;
}

 

posted on 2013-07-20 20:55  紫金树下  阅读(141)  评论(0编辑  收藏  举报