递归算法 - 高效
时间:2010-09-14 来源:嗯丶善解人衣
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace PersonalInterviewTest
{
public partial class PersonalInterviewTest : Form
{
public PersonalInterviewTest()
{
InitializeComponent();
skinEngine1.SkinFile = @"DiamondBlue.ssk";
}
static PersonalInterviewTest()
{
Values[1] = 1;
Values[2] = 1;
}
private static long[] Values = new long[93];
private void button1_Click(object sender, EventArgs e)
{
try
{
if (textBox2.Text == string.Empty)
{
MessageBox.Show("计算的内容不能为空。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var CirculateTime = new Stopwatch();
long Number = long.Parse(textBox2.Text);
CirculateTime.Start();
System.Threading.Thread.Sleep(1);
textBox1.Text = string.Format("第 {0} 位的结果是 {1}" + ",总耗时 {2} 毫秒。", Number.ToString(), PassToReturn(Number).ToString(), CirculateTime.ElapsedMilliseconds);
CirculateTime.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static long PassToReturn(long BeWorth)
{
if (BeWorth <= 0 || BeWorth >= 93)
{
throw new NotFiniteNumberException();
}
if (Values[BeWorth] == 0)
{
Values[BeWorth] = PassToReturn(BeWorth - 1) + PassToReturn(BeWorth - 2);
}
return Values[BeWorth];
}
}
}