首先碰到的是这样的一首题目:计算数组{1,1,2,3,5,8.......} 第30位值,不用递归,我写出了以下这样的代码:
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int[] num=new int[30];
num[0]=1;
num[1]=1;
int first=num[0];
int second=num[1];
for (int i = 2; i < num.Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
num[i] = first + second;
first = second;
second = num[i];
}
Console.WriteLine(num[29]);
Console.ReadLine();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
写出来,十分的累赘,于是改为归递算法来写,一目了然,十分明了。以下是代码:
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine(Process1(30));
Console.ReadLine();
}
public static int Process1(int i)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//计算数组{1,1,2,3,5,8.......} 第30位值
if (i == 0) return 0;
if (i == 1) return 1;
else
return Process1(i - 1) + Process1(i - 2);
}
做了一些练习:
1. 计算1+2+3+4+...+100的值
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
Console.WriteLine(Process2(100));
Console.ReadLine();
}
public static int Process2(int i)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
//计算1+2+3+4+...+100的值
if (i == 0) return 0;
return Process2(i - 1) + i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
2. 计算1 -2 +3 +-4+ 5- 6 + 7 - 8 + 9的值
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine(Process3(9) - Process3(8));
Console.ReadLine();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static int Process3(int i)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
//计算1 -2 +3 +-4+ 5- 6 + 7 - 8 + 9的值
if (i == 0) return 1;
if (i == 1) return 2;
else return Process3(i - 2) + i;
}
3.汉诺塔问题
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
Hanoi(5, 'A', 'B', 'C');
Console.ReadLine();
}
public static void Hanoi(int n ,char A, char B, char C)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
//汉诺塔问题
//将n个盘子从A座借助B座,移到C座
if (n == 1) Move(A, C);
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Hanoi(n - 1, A, C, B);
Move(A, C);
Hanoi(n - 1, B, A, C);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
public static void Move(char startPlace, char endPlace)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
Console.WriteLine("Move {0} To {1}",startPlace,endPlace);
}
4.用递归法将一个整数n转换成字符串,例如,输入483,就输出字符串"483".n的位数不确定,可以是任意位数的整数。
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
IntToString(483, "");
Console.ReadLine();
}
public static void IntToString(int input,String output)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
//用递归法将一个整数n转换成字符串,例如,输入483,就输出字符串"483".n的位数不确定,可以是任意位数的整数。
// String output = "";
output = input % 10+output;
if (input / 10 != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
IntToString(input / 10,output);
}
else Console.WriteLine(output);
}