函数复习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 习题一
{
    class Program
    {
        //卖羊:一个人赶了一群羊,每过一个村庄卖掉这群羊的二分之一外加一只,过了七个村庄之后
        //还剩两只,问最初赶了多少只羊
        public int yang(int b) 
        {
            for (int i = 8; i > 1; i--)//倒着往回推,过了7个村庄后剩两只,从8开始算起
            {
                b = (b + 1) * 2;
            }
            return (b);
        }
        static void Main(string[] args)
        { 
            Program hanshu = new Program();
            int a = 2;
            a=hanshu.yang(a);
            Console.WriteLine(a);  
            Console.ReadLine();
        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 生兔子
{
    class Program
    {
        //有一对幼兔,幼兔1个月后长成小兔,小兔一个月后长成成兔并生下一对幼兔,问几年后有多少对兔子,
        //幼兔、小兔、成兔对数分别是多少,成兔每月生下一对幼兔。
        //本月的成兔=上月的小兔+上月的成兔。
        //本月的幼兔=本月的成兔。
        public void z(int y)
        {
            int ct = 0;//成兔的对数ct
            int xt = 0;//小兔的对数xt
            int yt = 1;//幼兔的对数yt
            int zt = 1;//总兔的对数zt
            for (int i = 1; i <= y; i++) 
            {
                if (i == 1)//定义第一个月的成兔幼兔小兔对数
                {
                    //告诉电脑第一个月时兔子的对数
                    ct = 0;//第一个月成兔对数=0
                    xt = 0;//第一个月小兔对数=0
                    yt = 1;//第一个月幼兔对数=1
                }
                else 
                {
                    ct = xt + ct;//每月成兔对数=上月的小兔对数+上月的成兔对数
                    xt = yt;//每月小兔对数=上月幼兔对数
                    yt = ct;//每月幼兔对数=本月成兔对数
                }
                zt = ct + xt + yt;//总兔对数等于成兔加小兔加幼兔
                Console.WriteLine(i+"个月后成兔的对数是"+ct);
                Console.WriteLine(i + "个月后小兔的对数是" + xt);
                Console.WriteLine(i + "个月后幼兔的对数是" + yt);
                Console.WriteLine(i + "个月后总兔的对数是" + zt);
            }
        }
        static void Main(string[] args)
        {
            Program hanshu = new Program();
            Console.Write("请输入月数:");
            int m = int.Parse(Console.ReadLine());
            hanshu.z(m);

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading.Tasks;

namespace 练习三
{
    class Program
    {
        public void renshu(int m)
        {
            //输入人数,再输入每一个人的分数,求平均分,进行排序,升序,降序
            ArrayList al = new ArrayList();
            double sum = 0;
            for (int i = 0; i < m; i++)
            {
                Console.Write("请输入第" + (i + 1) + "个人的分数:");
                al.Add(double.Parse(Console.ReadLine()));
                sum += double.Parse(al[i].ToString());
            }
            Console.WriteLine("平均成绩为" + sum / m + "");
            al.Sort();
            foreach (object a in al)
            {
                Console.Write(a + "\t");
            }
            Console.WriteLine();
            al.Reverse();
            foreach (object b in al)
            {
                Console.Write(b + "\t");
            }
        }
        static void Main(string[] args)
        {
            Program hanshu = new Program();
            Console.Write("请输入班级人数:");
            int n = int.Parse(Console.ReadLine());
            hanshu.renshu(n);
            Console.ReadLine();
        }
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 练习题_篮球
{
    class Program
    {
        //写个函数,要求计算篮球的弹起高度,从20米落下,这次弹起的高度是上次的四分之三
        //求输入次数计算出高度     有参数有返回值
        public double c(int a) 
        {
            double h = 20;
            for (int i = 1; i <= a; i++)
            {
                h *= 0.75;
            }
            return (h);
        }
        static void Main(string[] args)
        {
            Program hs = new Program();
            Console.Write("输入弹起次数:");
            int n = int.Parse(Console.ReadLine());    
            Console.WriteLine(hs.c(n));
            Console.ReadLine();
        }
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 函数练习_双色球
{
    class Program
    {
        //随机出现32以内的六个红色球,随机出现16以内的蓝色球一个,组成彩票号码
        //没有参数,没有返回值
        public int[] caipiao()
        {
            int[] aa = new int[7];
            Random ran = new Random();
            for (int i = 0; i < 6; i++)
            {
                int a = ran.Next(1, 33);
                aa[i] = a;
                for (int l = 0; l < i; l++)
                {
                    if (aa[l] == a)
                    {
                        i--;
                    }
                }
            }
            aa[6] = ran.Next(1, 17);
            return aa;
        }
        static void Main(string[] args)
        {
            Program hanshu = new Program();
            int[] a = new int[7];
            a = hanshu.caipiao();
            foreach (int b in a)
            {
                Console.Write(b + "\t");
            }
            Console.ReadLine();
        }
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 函数练习_打印等腰三角形
{
    class Program
    {
        //写一个等腰三角形的函数
        //有输入值,没有返回值
        public void sanjiao(int a) 
        {
            for (int i = 1; i <= a; i++)
            {
                for (int j = a; j > i; j--)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= i; k++)
                {
                    Console.Write("");
                }
                Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            Console.Write("请输入一个整数:");
            int n=int.Parse(Console.ReadLine());
            Program hanshu = new Program();
            hanshu.sanjiao(n);
            Console.ReadLine();
        }
    }
}

 

posted @ 2016-05-13 02:24  小飛  阅读(188)  评论(0编辑  收藏  举报