要求不能使用for,while,switch等条件判断与乘除运算。
这题属于典型的脑筋急转弯题,这里可以使用逻辑运算符短路,来设计solution。
例如
a && b
如果a为假,则直接挑过后面的式子。
#include <iostream>
using namespace std;
int sum_caculate(int n){
int res = n;
res && (res += sum_caculate(n-1));
return res;
}
int main()
{
cout<<sum_caculate(10)<<endl;
return 0;
}