• C#递归算法


    原文地址:http://www.cnblogs.com/tygwy/archive/2010/04/28/1723054.html

    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);

            }

  • 相关阅读:
    DBG
    gdb Debugging Full Example
    Java Warmup
    Dtrace for Linux 2016
    分布式系统理论进阶
    Java theory and practice
    Dealing with InterruptedException
    JVM 虚拟化
    Intro to Filtering with Network Monitor 3.0
    spring 官方文档
  • 原文地址:https://www.cnblogs.com/niaowo/p/3822281.html
Copyright © 2020-2023  润新知