函数练习

1、兔子问题的函数做法

主要代码:

        static void Main(string[] args)
        {
            Program method = new Program();
            Console.Write("请输入月数:");
            int mouth = int.Parse(Console.ReadLine());
            Console.WriteLine(method.rabbit(mouth));
            Console.ReadLine();           
        }
        public int rabbit(int mouth)
        {
            int y = 1;
            int x = 0;
            int c = 0;
            int s = 1;
            if (mouth >= 1 && mouth <= 12)
            {
                for (int i = 1; i <= mouth; i++)
                {
                    if (i == 1)
                    {
                        y = 1;
                        x = 0;
                        c = 0;
                    }
                    else
                    {
                        c += x;
                        x = y;
                        y = c;
                    }
                    s = c + x + y;
                }
            }
            return s;
        }                     

结果:

2、卖养,每过一个村庄卖掉总数的二分之一零一只,过了七个村庄后还有两只,问最初有多少只羊。

主要代码:

        static void Main(string[] args)
        {
            Program method = new Program();
            Console.WriteLine(method.goat(2));
            Console.ReadLine();
        }
        public int goat(int num)
        {
            for (int i = 8; i > 1; i--)
                num = (num + 1) * 2;
            return num;
        }

结果:

3、篮球从20米落下,反弹原来的3/4,输入弹起次数,算出总长度(有返回值,有参数)

主要代码:

        static void Main(string[] args)
        {
            Program function = new Program();
            Console.Write("请输入弹起次数:");
            int n = int.Parse(Console.ReadLine());
            double h = 20;
            Console.WriteLine(function.hight(h, n));
            Console.ReadLine();
        }
        public double hight(double h, int c)
        {
            double sum = 0;
            for (double i = 0; i < c; i++)
            {
                sum += 2 * h;
                h *= 0.75;
            }
            sum -= 20;
            return sum;
        }

结果:

4、随机出现32以内的6个红球,随机出现16以内的1个蓝球,组成彩票号码(无参数,有返回值)

主要代码:

        static void Main(string[] args)
        {
            Program function = new Program();
            int[] lottery = function.lottery();
            foreach (int a in lottery)
                Console.Write(a + "\t");
            Console.ReadLine();
        }
        public int[] lottery()
        {
            Random red = new Random();
            int[] array = new int[7];
            for (int i = 0; i < 6; i++)
            {
                int Red = red.Next(1, 33);
                array[i] = Red;
                for (int j = 0; j < i; j++)
                {
                    if (array[j] == Red)
                        i--;
                }
            }
            Random blue = new Random();
            int Blue = blue.Next(1, 17);
            array[6] = Blue;
            return array;
        }

结果:

5、打印等腰三角形

主要代码:

        static void Main(string[] args)
        {
            Program function = new Program();
            Console.Write("请输入行数:");
            int h = int.Parse(Console.ReadLine());
            function.triangle(h);
            Console.ReadLine();
        }
        public void triangle(int h)
        {
            for (int i = 0; i <= h; i++)
            {
                for (int j = h - 1; j >= i; j--)
                    Console.Write(" ");
                for (int k = 0; k < i; k++)
                    Console.Write("*");
                for (int n = 0; n < i - 1; n++)
                    Console.Write("*");
                Console.WriteLine();
            }
        }

结果:

posted on 2016-05-13 20:33  bosamvs  阅读(112)  评论(0编辑  收藏  举报

导航