您的位置:首页 > 其它

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

2013-05-03 11:11 1286 查看
一个方法是递归的,另一个值返回常量值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));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐