[收藏]递归算出两个数字之间所有数字之和
递归算出两个数字之间所有数字之和
Code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace AspNetTest.Common
{
/// <summary>
/// AddFrom1To100 的摘要说明。
/// </summary>
public class AddFrom1To100 : System.Web.UI.Page
{
private const int from = 1;
private const int to = 100;
int sum = 0;
private void Page_Load(object sender, System.EventArgs e)
{
f1(from, to);
Response.Write(sum.ToString() + "<br>");
sum = 0;
f2(to, from);
Response.Write(sum.ToString() + "<br>");
sum = f3(from, to);
Response.Write(sum.ToString() + "<br>");
sum = f4(to, from);
Response.Write(sum.ToString() + "<br>");
sum = 0;
f5(from, to);
Response.Write(sum.ToString() + "<br>");
sum = 0;
f6(to, from);
Response.Write(sum.ToString() + "<br>");
// 在此处放置用户代码以初始化页面
}
private int f1(int _from, int _to)
{
int n = 0;
if (_from == _to)
{
n = _to;
}
else
{
n = f1(_from + 1, _to) - 1;
}
sum += n;
return n;
}
private int f2(int _to, int _from)
{
int n = 0;
if (_to == _from)
{
n = _from;
}
else
{
n = f2(_to - 1, _from) + 1;
}
sum += n;
return n;
}
private int f3(int _from, int _to)
{
if (_from == _to)
{
return _to;
}
return (_from + f3(_from + 1, _to));
}
private int f4(int _to, int _from)
{
if (_to == _from)
{
return _from;
}
return (_to + f4(_to - 1, _from));
}
private void f5(int _from, int _to)
{
if (_from <= _to)
{
sum += _from;
f5(_from + 1, _to);
}
}
private void f6(int _to, int _from)
{
if (_to >= _from)
{
sum += _to;
f6(_to - 1, _from);
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace AspNetTest.Common
{
/// <summary>
/// AddFrom1To100 的摘要说明。
/// </summary>
public class AddFrom1To100 : System.Web.UI.Page
{
private const int from = 1;
private const int to = 100;
int sum = 0;
private void Page_Load(object sender, System.EventArgs e)
{
f1(from, to);
Response.Write(sum.ToString() + "<br>");
sum = 0;
f2(to, from);
Response.Write(sum.ToString() + "<br>");
sum = f3(from, to);
Response.Write(sum.ToString() + "<br>");
sum = f4(to, from);
Response.Write(sum.ToString() + "<br>");
sum = 0;
f5(from, to);
Response.Write(sum.ToString() + "<br>");
sum = 0;
f6(to, from);
Response.Write(sum.ToString() + "<br>");
// 在此处放置用户代码以初始化页面
}
private int f1(int _from, int _to)
{
int n = 0;
if (_from == _to)
{
n = _to;
}
else
{
n = f1(_from + 1, _to) - 1;
}
sum += n;
return n;
}
private int f2(int _to, int _from)
{
int n = 0;
if (_to == _from)
{
n = _from;
}
else
{
n = f2(_to - 1, _from) + 1;
}
sum += n;
return n;
}
private int f3(int _from, int _to)
{
if (_from == _to)
{
return _to;
}
return (_from + f3(_from + 1, _to));
}
private int f4(int _to, int _from)
{
if (_to == _from)
{
return _from;
}
return (_to + f4(_to - 1, _from));
}
private void f5(int _from, int _to)
{
if (_from <= _to)
{
sum += _from;
f5(_from + 1, _to);
}
}
private void f6(int _to, int _from)
{
if (_to >= _from)
{
sum += _to;
f6(_to - 1, _from);
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}