简体   繁体   English

显示10个输入数字的总和,但取出最高和最低数字

[英]Display sum of 10 input numbers but take out highest and lowest number

I want to input 10 numbers and get the total sum of them if you take out the highest and lowest number that was input. 我想输入10个数字并获取它们的总和(如果您减去输入的最高和最低数字)。 So basically it'll be 8 numbers that i get the sum of when i take out the highest and lowest number that was input out of the 10. So far i can only count the total sum out of the 10 numbers, but not sure how to take out the highest and lowest. 所以基本上是8个数字,当我从10个数字中取出输入的最高和最低数字时,我得到的总和。到目前为止,我只能从10个数字中算出总和,但不确定如何拿出最高和最低。 What approach could i take? 我可以采取什么方法?

static void Main(string[] args)
        {
            int number, sum = 0, n;

            for (number = 1; number < 11; number++)
            {
                Console.WriteLine("Enter a number");
                n = Convert.ToInt32(Console.ReadLine());
                sum += n;
            }

            Points(sum);
            Console.ReadLine();

        }

        static void Points(int sum)
        {
            Console.WriteLine("Totalpoint is " + sum);
        }

An easy implementation to explain my thought process, could be the following: 一个简单的实现方式可以解释我的思考过程,如下所示:

static void Main(string[] args)
{

    int number, sum = 0, n;

    List<int> inputNumbers = new List<int>();
    for(number = 0; number < 10; number++)
    {
        inputNumbers.Add(Convert.ToInt32(Console.ReadLine()));
    }

    // Obtain maximum and minimum values
    var maximum = inputNumbers.Max();
    var mimimum = inputNumbers.Min();

    foreach (var item in inputNumbers)
    {
        if(item == maximum)
        {
            inputNumbers.Remove(item);
            break;
        }
    }

    foreach(var item in inputNumbers)
    {
        if(item == mimimum)
        {
            inputNumbers.Remove(item);
            break;
        }
    }

    Console.WriteLine("Sum: "+inputNumbers.Sum());
    Console.ReadKey();
}

The solution is rather simple, since after populating the List with the specified 10 values, you query it to obtain the minimum and maximum values. 解决方案非常简单,因为在使用指定的10个值填充列表之后,您可以查询该列表以获得最小值和最大值。 When you know them, you can simply loop through the list and remove both the maximum and minimum (break is completely necessary after those operations). 当您知道它们时,您可以简单地遍历列表并删除最大值和最小值(在这些操作之后完全有必要中断)。

Downside? 缺点? In this simple implementation, you would be iterating through the List two times, which should definitely be optimized, even if for clean code sake. 在这个简单的实现中,您将遍历List两次,即使出于简洁的代码考虑,也应该对其进行优化。 But it might the best first step to you! 但这可能是您最好的第一步!

Addition is additive, so you can simply remove them on the end: 添加是累加的,因此您可以在最后简单地将它们删除:

int sum = 0;
int min = int.MaxValue;
int max = int.MinValue;

for (int i = 0; i < 10; ++i)
{
    Console.WriteLine("Enter a number");
    int n = Convert.ToInt32(Console.ReadLine());
    sum += n;
    min = Math.Min(min, n);
    max = Math.Max(max, n);
}

sum -= min;
sum -= max;

Points(sum);
Console.ReadLine();

Just have two more variables to track max and min values as you read them: 阅读它们时,只需再有两个变量即可跟踪最大值和最小值:

min = Math.Min(min, n);
max = Math.Max(max, n);

Then, as you exit the loop, you can simply subtract those from the total sum before printing. 然后,当退出循环时,您可以简单地从总和中减去这些,然后再打印。

sum -= min + max;

You can try this: 您可以尝试以下方法:

List<int> testNum = new List<int>();
int num = 0;
int sum = 0;
try
{
   // This will loop until you have 10 inputs
   while(testNum.Count < 10)
   {
       Console.WriteLine("Enter a number:");
       num = Convert.ToInt32(Console.ReadLine()); // Need to have the Try/Catch in case user doesn't input a number
       testNum.Add(num);
   }

   // Get the min and max values (you will need System.Linq for this)
   int minVal = testNum.Min();
   int maxVal = testNum.Max();

   // Remove the min and max values from the List
   testNum.Remove(testNum.IndexOf(minVal));
   testNum.Remove(testNum.IndexOf(maxVal));

   // Sum the remaining values up
   foreach(int n in testNum)
   {
       sum = sum + n;
   }

   return sum;

}
catch
{
    throw;
}

Add the input numbers in the List and sum their values excluding min and max values 将输入数字添加到列表中,并求和它们的值,不包括最小值和最大值

public  static void Main(string[] args)
    {
        var list = new List<int>();
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("Enter number " + i);
            int num = Convert.ToInt32(Console.ReadLine());
            list.Add(num); //adding the input number in list
        }
        Sum(list);           
    }

  private static void Sum(List<int> list)
  {
      int max =  list.Max();
      int min = list.Min();
      int sum = list.Where(x => x != max && x != min).Sum(); //taking the sum of all values exlcuding min and max
      Console.WriteLine("Sum is " + sum);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM