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

    利用&&的“短路”特性:&&根据第一个条件的结果来决定是否执行第二个条件。如果第一个条件是false,就不用再执行后面的条件。

    而&的特性是两边的条件都要判断(无论前面是true还是flase)。

    方法一:

  private static int GetSum(int n)
  {
         int num = 0;
         DoNothing((n > 0) && Convert.ToBoolean(num = n + GetSum(n - 1)));//如果不调用DoNothing()程式就会报错
         return num;
   }

  private static void DoNothing(bool isTruth)
  { 
        
  }

  方法二(直接根据数学公式得出):

private int GetSum(int n)
{
     return n*(n-1)/2;
}

  

 

 posted on 2015-10-31 16:59  会飞的金鱼  阅读(126)  评论(0)    收藏  举报