zoukankan      html  css  js  c++  java
  • C#算法 有一个母羊,第2年和第4年可以生一头小母羊,在第5年死去,小母羊在它出生的第2年和第4年生小母羊,第5年死去

         /// <summary>
        /// 母羊类
        /// 有一个母羊,第2年和第4年可以生一头小母羊,在第5年死去,小母羊在它出生的第2年和第4年生小母羊,第5年死去
        /// </summary>
        class Sheep
        {
            public int year;
    
            public Sheep(int year)
            {
                this.year = year;
            }
    
            /// <summary>
            /// 方法一 多函数递归
            /// 获得母羊总数目
            /// 母羊第2年和第4年可以生一头小母羊,在第5年死去。
            /// </summary>
            /// <returns></returns>
            public int GetSheep(int year)
            {
                return GetSheepofAge1(year) + GetSheepofAge2(year) + GetSheepofAge3(year) + GetSheepofAge4(year); 
            }
    
            /// <summary>
            /// 获得一岁的母羊
            /// 一岁的母羊只能是去年一岁的母羊生的母羊和去年三岁的母羊生的母羊
            /// </summary>
            /// <returns></returns>
            public int GetSheepofAge1(int year)
            {
                if (year <= 1)
                    return 1;
                else
                    return GetSheepofAge1(year - 1) + GetSheepofAge3(year - 1);
            }
    
            /// <summary>
            /// 获得二岁的母羊
            /// 二岁的母羊只能是去年一岁的母羊
            /// </summary>
            /// <param name="year"></param>
            /// <returns></returns>
            public int GetSheepofAge2(int year)
            {
                if (year <= 1)
                    return 0;
                else
                    return GetSheepofAge1(year - 1);
            }
    
            /// <summary>
            /// 获得三岁的母羊
            /// 三岁的母羊只能是去年二岁的母羊
            /// </summary>
            /// <param name="year"></param>
            /// <returns></returns>
            public int GetSheepofAge3(int year)
            {
                if (year <= 2)
                    return 0;
                else
                    return GetSheepofAge2(year - 1);       
            }
    
            /// <summary>
            /// 获得四岁的母羊
            /// 四岁的母羊只能是去年三岁的母羊
            /// </summary>
            /// <param name="year"></param>
            /// <returns></returns>
            public int GetSheepofAge4(int year)
            {
                if (year <= 3)
                    return 0;
                else
                    return GetSheepofAge3(year - 1);
            }
    }

    以上 多函数递归 算法转自http://www.mianwww.com/html/2012/03/13780.html

            /// <summary>
            /// 方法二 一个函数递归
         /// </summary>
            /// <param name="year"></param>
            /// <returns></returns>
            public int CalcSheep(int year)
            {
                if (year < 1)
                    return 0;
                if (year == 1)
                    return 1;
                if (year == 2)
                    return 2;
                if (year == 3)
                    return 3;
                else
                    return (year >= 5 ? 0 : 1) + CalcSheep(year - 1) + CalcSheep(year - 3);
            }
    Sheep sheep = new Sheep(15);
     int sheepNum = sheep.GetSheep(sheep.year);
    Console.WriteLine("方法一,第15年共有{0}只母羊!", sheepNum);
    int num = sheep.CalcSheep(sheep.year);
    Console.WriteLine("方法二,第15年共有{0}只母羊!", num);
  • 相关阅读:
    Mysql性能优化之---(一)
    好好思考之(二)---介绍什么是思维模型,分析它的本质内涵
    好好思考-----概述(一)
    oracle 的安装 及环境的配置...
    数据结构中的树
    生成SSH秘钥连接github(详细教程)
    后端 SpringBoot + 前端 vue 打包发布到Tomcat
    vue 轮播图
    vue文字向上滚动
    数组去重
  • 原文地址:https://www.cnblogs.com/YuanSong/p/2711343.html
Copyright © 2011-2022 走看看