《剑指offer》------求1+2+3+···+n

题目:

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

解答:

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

#include <iostream>
using namespace std;
int Sum_Solution(int n){
    int sum = n;
    sum&&(sum+=Sum_Solution(n-1));
    return sum;
}
int main()
{
    cout<<Sum_Solution(2)<<endl; 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hglibin/p/8976149.html
今日推荐