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);
  • 相关阅读:
    2-sat问题,输出方案,几种方法(赵爽的论文染色解法+其完全改进版)浅析 / POJ3683
    hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。
    最小费用最大流粗解 poj2516
    hdu3078 建层次树+在线LCA算法+排序
    hdu 3594 Cactus /uva 10510 仙人掌图判定
    有向图最小路径覆盖方法浅析、证明 //hdu 3861
    hdu 1827 有向图缩点看度数
    条件转化,2-sat BZOJ 1997
    2-sat基础题 BZOJ 1823
    2-sat 分类讨论 UVALIVE 3713
  • 原文地址:https://www.cnblogs.com/YuanSong/p/2711343.html
Copyright © 2011-2022 走看看