简体   繁体   English

如何获得最小的输入数字总和?

[英]How do i get the lowest sum of input numbers?

I've got a task for printing the highest sum of numbers, lowest sum of numbers and the average of numbers which were printed.我的任务是打印最高的数字总和、最低的数字总和和打印的平均数字。 i managed to get the higest sum of numbers but with the lowest somehow didn't work for me at all.我设法获得了最大的数字总和,但以某种方式最低的数字对我来说根本不起作用。

for example: 7 3 -2 6 -10 8 -5 3 -2 1例如:7 3 -2 6 -10 8 -5 3 -2 1

their Interim amount: 7 10 8 14 4 12 7 10 8 9他们的中期金额:7 10 8 14 4 12 7 10 8 9

the output will be: output 将是:

Higest Interim amount: 14最高中期金额:14

Lowest Interim amount: 4最低中期金额:4

Avg of numbers: 0.9平均数字:0.9

my code:我的代码:

static void Main(string[] args)
        {
            int num = 0, i, sum = 0, c = 0, max, min;
            max = min = num;
            for (i = 0; i <= 10; i++)
            {
                Console.WriteLine("Enter a number:");
                num = int.Parse(Console.ReadLine());
                sum = sum + num;
                if (sum >= max)
                    max = sum;
                else if (sum <= min)
                    min = sum;
                c++;
                Console.WriteLine("sum:{0}", sum);
            }
            Console.WriteLine("count={0} avg={1}", c, sum / (float)c);
            Console.WriteLine("Max:{0} Min:{1}", max, min);
        }

You make both min and max start at 0, so the min will only update if you get a negative value for the cumulative sum (which does not happen in your example).您使最小值和最大值都从 0 开始,因此只有在累积总和为负值时才会更新最小值(这在您的示例中不会发生)。 (Would be the same for the max value if all your sums remained negative). (如果您的所有总和均为负数,最大值将相同)。

You need to start with the numeric equivalent of min = +Infinity and max = -Infinity.您需要从 min = +Infinity 和 max = -Infinity 的数值等价物开始。

ie IE

static void Main(string[] args)
        {
            int num = 0, i, sum = 0, c = 0;
            int max = int.MinValue;
            int min = int.MaxValue;

(Thanks @Caius Jard for the syntax, I know nothing about C#) (感谢@Caius Jard 的语法,我对 C# 一无所知)

It's because you set the initial min to 0, and then only set the minimum to the sum if it drops below that.这是因为您将初始min设置为 0,然后只有在低于该值时才将最小值设置为总和。 If you set initial minimum and maximum to the first number input you won't have this problem:如果将初始最小值和最大值设置为第一个数字输入,则不会出现此问题:

static void Main(string[] args)
        {
            int num = 0, i, sum = 0, c = 0, max, min;
            for (i = 0; i <= 9; i++)
            {
                Console.WriteLine("Enter a number:");
                num = int.Parse(Console.ReadLine());
                if(i == 0)
                {
                     min = num;
                     max = num;
                }
                sum = sum + num;
                if (sum >= max)
                    max = sum;
                else if (sum <= min)
                    min = sum;
                c++;
                Console.WriteLine("sum:{0}", sum);
            }
            Console.WriteLine("count={0} avg={1}", c, sum / (float)c);
            Console.WriteLine("Max:{0} Min:{1}", max, min);
        }

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

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