求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句

一个方法是递归的,另一个值返回常量值1,就是把递归中的判断改成了一个返回值始终是1的方法。

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 递归算法
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 3;
            int result = Sum(num);
        }

        protected internal static int Sum(int num)
        {
            if (num == 1)
                return num;
            else
                return (num += Sum(num - 1));
        }
    }
}
原文地址:https://www.cnblogs.com/binyao/p/3056275.html