求 1+2+...+n, 要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句 (A?B:C)。

求 1+2+...+n,
要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句 (A?B:C)。

#include <bits/stdc++.h>

using namespace std;

int Sum(int n)
{
    int Ret = 0;
    n == 0 || (Ret = Sum(n-1));
    return n + Ret;
}

class A{
    public:
    A()
    {
        sum += ++n;
    }
    static int sum;
    static int n;
};
int A::sum = 0;
int A::n = 0;
int main()
{
    int ans = Sum(10);
    cout << ans << endl;
    A a[10];
    cout << A::sum << endl;
 } 

 

posted on 2017-04-14 08:52  yifi  阅读(419)  评论(0编辑  收藏  举报

导航