编写C#程序,计算去除最大值和最小值之后的平均值

有10位评委对跳水运动员做评分,编写C#程序,计算去除最大得分和最小得分之后的平均得分 作为运动员的跳水成绩。

interface IMark

using System.Collections.Generic;

namespace ConsoleApp1
{
    /// <summary>
    /// 评分接口
    /// </summary>
    interface IMark
    {

        Mark GetMaxScore(List<Mark> list_Marks);

        Mark GetMinScore(List<Mark> list_Marks);

        /// <summary>
        /// 获取所有评分
        /// </summary>
        /// <returns></returns>
        List<Mark> GetAllMarks(int athleteId);

        /// <summary>
        /// 获取除最大得分和最小得分之后的平均得分
        /// </summary>
        /// <returns></returns>
        float GetAvgScoreExceptFirstAndLast();
    }
}

class Mark

namespace ConsoleApp1
{
    /// <summary>
    /// 评分实体
    /// </summary>
    public class Mark
    {
        public int Id;
        public int JuryId;
        public int AthleteId;
        public float Score;
    }
}

class MarkService

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    /// <summary>
    /// 实现评分接口
    /// </summary>
    public class MarkService : IMark
    {  
        public MarkService()
        { 
        }

        /// <summary>
        /// 获取所有评分
        /// </summary>
        /// <param name="athleteId"></param>
        /// <returns></returns>
        public List<Mark> GetAllMarks(int athleteId)
        {
            List<Mark> list = new List<Mark>();
            for (int i = 1; i < 11; i++) {
                list.Add(new Mark()
                {
                    Id = i,
                    JuryId = i,
                    AthleteId = 1,
                    Score = i,                    
                });
            }
            return list.Where(x=>x.AthleteId ==athleteId).ToList();
        } 

        /// <summary>
        /// 获取除最小分数、最大分数之外的平均得分
        /// </summary>
        /// <returns></returns>
        public float GetAvgScoreExceptFirstAndLast()
        {
            List<Mark> List_Marks = this.GetAllMarks(1);
            return List_Marks.Where(
                x => x.Id != this.GetMaxScore(List_Marks).Id &&
                    x.Id != this.GetMinScore(List_Marks).Id).Average(y=>y.Score); 
        }

        /// <summary>
        /// 获取最大得分
        /// </summary>
        /// <param name="list_Marks"></param>
        /// <returns></returns>
        public Mark GetMaxScore(List<Mark> list_Marks)
        {
            float maxScore = list_Marks.Max<Mark>(x => x.Score);
            return list_Marks.Where(x => x.Score == maxScore).FirstOrDefault();
        }

        /// <summary>
        /// 获取最小得分
        /// </summary>
        /// <param name="list_Marks"></param>
        /// <returns></returns>
        public Mark GetMinScore(List<Mark> list_Marks)
        {
            float minScore = list_Marks.Min<Mark>(x => x.Score);
            return list_Marks.Where(x => x.Score == minScore).FirstOrDefault(); 
        }

        
    }
}

 

class Program

using System;
using System.Text; 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            IMark t = new Test();
            Console.WriteLine(t.GetAvgScoreExceptFirstAndLast());

            Console.Read();

        }
    }
}

 

posted on 2017-07-12 15:43  Tencent/Tim  阅读(773)  评论(0编辑  收藏  举报

导航