zoukankan      html  css  js  c++  java
  • C# 递归算法!(n多举例) 转

              今天无所事事,于是重温了一下递归算法。突然之间发现递归算法很好用。

               首先碰到的是这样的一首题目:计算数组{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 == 0return 0;
                
    if (i == 1return 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 == 0return 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 == 0return 1;
                
    if (i == 1return 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);
            }
    add by inkstone  @2008年01月22日      

    转自:

    http://blog.csdn.net/inkstone2006/article/details/2057853

  • 相关阅读:
    运行hexo提示/usr/bin/env: node: 没有那个文件或目录
    安装cuda时 提示toolkit installation failed using unsupported compiler解决方法
    使用caffe自动测试模型top5的结果
    配置caffe的python环境时make pycaffe提示fatal error: numpy/arrayobject.h No such file or directory解决方法
    error: library dfftpack has Fortran sources but no Fortran compiler found解决方法
    ubuntu硬盘安装卡在探测文件系统
    win7+ubuntu双系统中卸载ubuntu方法
    深度学习入门教程UFLDL学习实验笔记三:主成分分析PCA与白化whitening
    深度学习入门教程UFLDL学习实验笔记二:使用向量化对MNIST数据集做稀疏自编码
    深度学习入门教程UFLDL学习实验笔记一:稀疏自编码器
  • 原文地址:https://www.cnblogs.com/liye/p/2433117.html
Copyright © 2011-2022 走看看