• 例题:青歌赛打分程序。去掉两个最高分,去掉两个最低分,再算平均分。主要学习数组和冒泡排序,什么情况下要定义数组


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

                int renshu = Convert.ToInt32(Console.ReadLine());//人数代入

                int[] fenshu = new int[renshu];//定义一个分数的数组,长度为人数

                if (renshu >= 5)//去掉连个最高分,去掉两个最低分,所以至少五个人

                {

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

                    {

                        Console.Write("请输入第" + (i + 1) + "个评委打的分数:");

                        fenshu[i] = Convert.ToInt32(Console.ReadLine());

                    }

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

                    {

                        for (int j = i; j < renshu - 1; j++)//冒泡排序,按分数从高到低排列

                        {

                            if (fenshu[i] < fenshu[j + 1])

                            {  

                               int zhong = 0;

                                zhong = fenshu[i];

                                fenshu[i] = fenshu[j + 1];

                                fenshu[j + 1] = zhong;

                            }

                        }

                    }

                    int he = 0;//计算总分,初始值为0

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

                    {

                        he += fenshu[i];//循环计算总分

                    }

                    double pjf = 0;//初始值为0

                    pjf = (he - fenshu[0] - fenshu[1] - fenshu[renshu - 1] - fenshu[renshu - 2]) / (renshu - 4);                 //计算平均分。fenshu[renshu - 1] , fenshu[renshu - 2]),这两个分别代表最低分,和倒数第二个最低分,可能不好理解。在上一步冒泡循环中,已经将分数排好。renshu代表下标的的长度,减一就是下标的最后一个位置,就是最低分

                    Console.WriteLine("去掉两个最高分" + fenshu[0] + "," + fenshu[1] + ",去掉两个最低分" + fenshu[renshu - 1] + "," + fenshu[renshu - 2] +                     "选手最后得分为" + pjf);

                }

                else

                {

                    Console.WriteLine("请输入至少五个人数");

                }

                Console.ReadLine();

  • 相关阅读:
    AC自动机
    KMP、扩展KMP、MANACHER
    Docker用户身份登录和管理员权限
    Response.Redirect和Server.Transfer比较--(转)
    SQLServer中char、varchar、nchar、nvarchar的区别--(转)
    MsSQL的字段类型--(转)
    读取UEditor编辑框内容到数据库和上传图片的配置
    以做产品的思想分析男女相处之道
    springBoot创建定时任务
    Runnable和Thread的区别
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4426326.html
Copyright © 2020-2023  润新知