`
lovnet
  • 浏览: 6704222 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

学习笔记23(C#递归详解)

 
阅读更多
首先碰到的是这样的一首题目:计算数组{1,1,2,3,5,8.......} 第30位值,不用递归,我写出了以下这样的代码:
static void Main(string[] args)
{
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++)
{
num[i] = first + second;
first = second;
second = num[i];
}
Console.WriteLine(num[29]);
Console.ReadLine();
}
写出来,十分的累赘,于是改为归递算法来写,一目了然,十分明了。以下是代码:
static void Main(string[] args)
{
Console.WriteLine(Process1(30));
Console.ReadLine();
}
public static int Process1(int i)
{
//计算数组{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)
{
Console.WriteLine(Process2(100));
Console.ReadLine();
}
public static int Process2(int i)
{
//计算1+2+3+4+...+100的值
if (i == 0) return 0;
return Process2(i - 1) + i;
}
2. 计算1 -2 +3 -4+ 5- 6 + 7 - 8 + 9的值
static void Main(string[] args)
{
Console.WriteLine(Process3(9) - Process3(8));
Console.ReadLine();
}
public static int Process3(int i)
{
//计算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)
{
Hanoi(5, 'A', 'B', 'C');
Console.ReadLine();
}
public static void Hanoi(int n ,char A, char B, char C)
{
//汉诺塔问题
//将n个盘子从A座借助B座,移到C座
if (n == 1) Move(A, C);
else
{
Hanoi(n - 1, A, C, B);
Move(A, C);
Hanoi(n - 1, B, A, C);
}
}
public static void Move(char startPlace, char endPlace)
{
Console.WriteLine("Move {0} To {1}",startPlace,endPlace);
}
4.用递归法将一个整数n转换成字符串,例如,输入483,就输出字符串"483".n的位数不确定,可以是任意位数的整数。
static void Main(string[] args)
{
IntToString(483, "");
Console.ReadLine();
}
public static void IntToString(int input,String output)
{
//用递归法将一个整数n转换成字符串,例如,输入483,就输出字符串"483".n的位数不确定,可以是任意位数的整数。
// String output = "";
output = input % 10+output;
if (input / 10 != 0)
{
IntToString(input / 10,output);
}
else Console.WriteLine(output);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C#递归的应用是可以让繁琐的问题的变得简单可执行,那么具体的实例就是打开文件的操作,我们知道获得某一目录下第一级的所有文件和文件夹列表,很容易办到:
DirectoryInfo di=new DirectoryInfo(strBaseDir);//strBaseDir是起始目录,绝对地址 DirectoryInfo[] diA=di.GetDirectories();//获得了所有一级子目录 FileInfo[] fiA=di.GetFiles();//获得了所有起始目录下的文件
要是想获得某一目录下的所有文件和目录(包含所有子目录),那该怎么办呢?目录都是一层套一层的,我们不能预知某个目录的深度,只有获得了父节点,才有可能了解子节点,解决这个问题,只有递归这个概念了。
C#递归的简单理解,它就是一个方法,在这个方法里面,再次调用它本身这个方法,从而描述了某一事物运作的深度…… - - 不废话了,看代码吧:
C#递归实现代码
public ArrayList al=new ArrayList(); //我把ArrayList当成动态数组用,非常好用 public void GetAllDirList(string strBaseDir) { DirectoryInfo di=new DirectoryInfo(strBaseDir); DirectoryInfo[] diA=di.GetDirectories();   for(int i=0;i<diA.Length;i++) { al.Add(diA[i].FullName); //diA[i].FullName是某个子目录的绝对地址,把它记录在ArrayList中 GetAllDirList(diA[i].FullName); //注意:这里使用C#递归的方法 } }
最后,如何把所有目录信息从ArrayList中取出来呢?如下:
for(int i=0;i<al.Count;i++) { textBox1.AppendText(al[i].ToString()+" "); //textBox1是容器,拷贝我的代码,注意要换一个你自己的容器 }
===========================================================================================
bs和CS方法用C#递归方法把数据表加载到treeview控件中
来源:不详 作者:佚名 日期:2010年05月18日 访问次数:41
先看一下数据库的结构:
表结构如下所示:
Num Name fatherNum BZ
01 总节点 0 ......
0101 第一个一级节点 01 ......
010101 第一个一级节点的第一个支节点 0101 ......
010102 第一个一级节点的第二个支节点 0101 ......
010103 第一个一级节点的第三个支节点 0101 ......
0102 第二个一级节点 01 ......
010201 第二个一级节点的第一个支节点 0102 ......
0103 第三个一级节点 01 ......
010301 第三个一级节点的第一个支节点 0103 ......
01030101 第三个一级节点的第一个支节点 010301 ......
C/S代码如下:
C/S代码 1 public void AddTree(string fatherNum, TreeNode pNode)
2 {
3
4 DataView dvTree = new DataView(ds.Tables[0]);
5
6 dvTree.RowFilter = "[father Num] = " + fatherNum;
7 foreach (DataRowView Row in dvTree)
8 {
9 TreeNode Node = new TreeNode();
10 if (pNode == null)
11 { //添加根节点
12 Node.Text = Row["Name"].ToString();
13
14 treeView1.Nodes.Add(Node);
15 AddTree(Row["Num"].ToString(), Node); //再次递归
16 }
17 else
18 { //添加当前节点的子节点
19 Node.Text = Row["Name"].ToString();
20 pNode.Nodes.Add(Node);
21
22 AddTree(Row["Num"].ToString(), Node); //再次递归
23 }
24 }
25 }
26
27 DataSet ds = new DataSet();
28 private void Form2_Load(object sender, EventArgs e)
29 {
30 SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=;");
31 SqlDataAdapter da = new SqlDataAdapter("select * from test",conn);
32 da.Fill(ds);
33 TreeNode pNode=null;
34 AddTree("0", pNode);
35 }
下面是B/S结构的代码:
B/S代码 1 DataSet ds = new DataSet();
2 protected void Page_Load(object sender, EventArgs e)
3 {
4 if (!IsPostBack)
5 {
6 SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=;");
7 SqlDataAdapter da = new SqlDataAdapter("select * from test", conn);
8 da.Fill(ds);
9
10 TreeNode pNode = null;
11 AddTree("0", pNode);
12 }
13
14 }
15 public void AddTree(string fatherNum, TreeNode pNode)
16 {
17 DataRow[] dr = ds.Tables[0].Select("fatherNum=" + fatherNum);
18 if (dr.Length > 0)
19 {
20 foreach (DataRow d in dr)
21 {
22 TreeNode tNode = new TreeNode();
23 tNode.Text = d["Name"].ToString();
24 if (pNode == null)
25 {
26 //添加根节点
27 TreeView1.Nodes.Add(tNode);
28 }
29 else
30 {
31 //添加当前节点的子节点
32 pNode.ChildNodes.Add(tNode);
33 tNode.Collapse();
34 }
35 AddTree(d["Num"].ToString(), tNode); //递归
36 }
37 }
38 }
39
===========================================================================================
SQL的递归查询
create procedure GetData(@a nvarchar(50))
as
begin
create table #Temp(
a nvarchar(50),
b nvarchar(50),
c nvarchar(50)
)
declare @tempB nvarchar(50)
insert into #Temp select a,b,c from T where a in(@a)
declare cur cursor for select b from #Temp
open cur
fetch next from cur into @tempB
while @@fetch_status = 0
begin
insert into #Temp select a,b,c from T where a in(@tempB)
fetch next from cur into @tempB
end
close cur
deallocate cur
select * from #Temp
end
=====================================================
上面的是假设表结构如下的:
a b c
=========================
1 2 aa
1 3 dfd
2 4 fdas
3 5 fds
4 7 fds
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics