码迷,mamicode.com
首页 > 编程语言 > 详细

20150915学习内容:数组、冒泡排序 及相关习题彩票等

时间:2015-10-08 14:24:16      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

   static void Main(string[] args)

        {

            #region

            /*

            while (true)

            {

            数组:一组同类型的数据,数组是有长度的,数组是有索引的,索引从0开始

=====彩票=====

                int[] shuzu = new int[7];//定义了一个长度为6的int类型的数组

                Random r = new Random();

 

                for (int i = 0; i < 6; i++)//循环生成六个数

                {

                    shuzu[i] = r.Next(1, 34);//生成一个数

 

                    bool isok = false;

                    for (int j = 0; j < i; j++)//比较是否跟之前的书相等

                    {

                        if (shuzu[j] == shuzu[i])

                        {

                            isok = true;

                        }

                    }

                    if (isok)

                    {

                        i--;//后退一步

                        continue;

                    }

 

                }

                shuzu[6] = r.Next(1, 17);

 

                //输入你的号码

                Console.Write("请输入红球6个,蓝球1个,逗号隔开:");

 

                string shuru = Console.ReadLine();

                string[] ren = shuru.Split(‘,‘);

                //判断中了几个红球

                int count = 0;

                for (int i = 0; i < 6; i++)

                {

                    for (int j = 0; j < 6; j++)

                    {

                        if (int.Parse(ren[i]) == shuzu[j])

                        {

                            count++;

                        }

                    }

                }

                //判断蓝球中没中

                bool islan = false;

                if (int.Parse(ren[6]) == shuzu[6])

                {

                    islan = true;

                }

 

                //输出电脑随机的中奖号码

                foreach (int a in shuzu)

                {

                    Console.Write(a + " | ");

                }

 

                //判断中几等奖

                if (count == 6 && islan)

                {

                    Console.Write("一等奖");

                }

                else if (count == 6 && !islan)

                {

                    Console.Write("二等奖");

                }

                else if (count == 5 && islan)

                {

                    Console.Write("三等奖");

                }

                else if ((count == 4 && islan) || (count == 5 && !islan))

                {

                    Console.Write("四等奖");

                }

                else if ((count == 3 && islan) || (count == 4 && !islan))

                {

                    Console.Write("五等奖");

                }

                else if ((count == 2 && islan) || (count == 1 && islan) || (count == 0 && islan))

                {

                    Console.Write("五块钱");

                }

                else

                {

                    Console.Write("别再买了");

                }      

                Console.ReadLine();

           }

             */

            #endregion

 

            /*

====输入10个人的分数,求最高分最低分平均分====

            int[] fenshu = new int[10];

            int max = 0;

            int min = 0;

            int sum = 0;

            for (int i = 0; i < 10; i++)

            {

                fenshu[i] = int.Parse(Console.ReadLine());

                sum += fenshu[i];

 

                if (i == 0)

                {

                    max = fenshu[i];

                    min = fenshu[i];

                }

                else

                {

                    if (max < fenshu[i])

                    {

                        max = fenshu[i];

                    }

                    if (min > fenshu[i])

                    {

                        min = fenshu[i];

                    }

                }

            }            

            Console.WriteLine("最大值:"+max);

            Console.WriteLine("最小值:" + min);

            Console.WriteLine("平均分:" + sum/10);

            */

 

====输入全班同学的年龄,按年龄从大到小排序=====

            Console.Write("请输入人数:");

            int n = int.Parse(Console.ReadLine());

 

            int[] nianling = new int[n];

            for (int i = 0; i < n; i++)

            {

                Console.Write("请输入第{0}个人的年龄:",i+1);

                nianling[i] = int.Parse(Console.ReadLine());

            }

 

            //开始排序

 

            for (int j = 0; j < n - 1; j++)

            {

                for (int i = j+1; i < n; i++)

                {

                    if (nianling[i] > nianling[j])

                    {

                        //等量代换

                        int zhong = nianling[i];

                        nianling[i] = nianling[j];

                        nianling[j] = zhong;

                    }

                }

            } 

            foreach (int i in nianling)

            {

                Console.WriteLine(i);

            }

                Console.ReadLine();

        }

20150915学习内容:数组、冒泡排序 及相关习题彩票等

标签:

原文地址:http://www.cnblogs.com/mn-b/p/4860712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!