求1+2+…+n,

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

方法:从递归下手

一般是这样的

int sum(int n){

      if(n==0)

           return 0;

      return n+sum(n-1);

}

把上述稍加改动即可

int sum(int n){

      return n&&(n+sum(n-1));

}

 

原文地址:https://www.cnblogs.com/GoAhead/p/2518247.html