爬楼梯问题的三种算法测试
爬楼梯问题,有n阶楼梯,每一步可以爬一个台阶或者2个台阶,总的爬法数?
例如1个台阶,1种爬法1;f(1)=1
例如2个台阶,2种爬法1+1,2;f(2)=2
例如3个台阶,3种爬法1+1+1,2+1,1+2;f(3)=f(2)+f(1)
例如4个台阶,5种爬法1+1+1+1,2+1+1,1+2+1,1+1+2,2+2;f(4)=f(3)+f(2)
......
例如n个台阶,爬法f(n)f(n-1)+f(n-2)
先看下运算结果:
楼梯个数=10
递归法: 结果=89 耗时=16 Ticks
字典递归法: 结果=89 耗时=337 Ticks
循环法: 结果=89 耗时=3 Ticks
楼梯个数=20
递归法: 结果=10946 耗时=1500 Ticks
字典递归法: 结果=10946 耗时=23 Ticks
循环法: 结果=10946 耗时=2 Ticks
楼梯个数=30
递归法: 结果=1346269 耗时=185763 Ticks
字典递归法: 结果=1346269 耗时=104 Ticks
循环法: 结果=1346269 耗时=3 Ticks
楼梯个数=40
递归法: 结果=165580141 耗时=23378518 Ticks
字典递归法: 结果=165580141 耗时=58 Ticks
循环法: 结果=165580141 耗时=3 Ticks
楼梯个数=50
递归法: 结果=-1109825406 耗时=2842699468 Ticks
字典递归法: 结果=-1109825406 耗时=148 Ticks
循环法: 结果=-1109825406 耗时=3 Ticks
递归法,时间复杂度O(n2),性能最差,
字典递归法,时间复杂度O(n),性能较好,需要进行字典数据查找和读写
循环法时间复杂度O(n),性能最好
三种算法
//简单的递归算法
public static int UpStairInteration(int n)
{
if (n == 1) return 1;
if (n == 2) return 2;
return UpStairInteration(n - 1) + UpStairInteration(n - 2);
}
//递归+字典算法
public static int UpStairDictionary(int n, Dictionary<int, int> upStairDic)
{
if (n == 1) return 1;
if (n == 2) return 2;
if (upStairDic.ContainsKey(n)) return upStairDic[n];
int result = UpStairDictionary(n - 1, upStairDic) + UpStairDictionary(n - 2, upStairDic);
upStairDic.Add(n, result);
return result;
}
//循环算法
public static int UpStairFor(int n)
{
if (n == 1) return 1;
if (n == 2) return 2;
int result = 0;
int p = 2, pp = 1;
for (int i = 3; i <= n; i++)
{
result = p + pp;
pp = p;
p = result;
}
return result;
}
测试
static void test8()
{
UpStairTest(1);
for (int i = 10; i <= 100; i += 10)
{
UpStairTest(i);
}
}
static void UpStairTest(int n)
{
Dictionary<int, int> upStairDic = new Dictionary<int, int>();
Console.WriteLine($"楼梯个数={n}");
int result = 0;
Stopwatch watch = new Stopwatch();
watch.Start(); watch.Stop();
watch.Restart();
result = UpStairInteration(n);
watch.Stop();
Console.WriteLine($"递归法: 结果={result} 耗时={watch.ElapsedTicks} Ticks");
watch.Restart();
result = UpStairDictionary(n, upStairDic);
watch.Stop();
Console.WriteLine($"字典递归法: 结果={result} 耗时={watch.ElapsedTicks} Ticks");
watch.Restart();
result = UpStairFor(n);
watch.Stop();
Console.WriteLine($"循环法: 结果={result} 耗时={watch.ElapsedTicks} Ticks");
Console.WriteLine();
}
运行结果