要求不能使用for,while,switch等條件判斷與乘除運算。
這題屬于典型的腦筋急轉(zhuǎn)彎題,這里可以使用邏輯運算符短路,來設(shè)計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;
}