如何从数组中的两个位置求出大于或等于17的数字?

时间:2019-08-26 10:05:48

标签: c#

我有一个具有以下值的数组:

var values = new [] { 13, 18, 13, 12, 13, 17, 17, 18, 19, 20, 18, 17, 17, 12, 13, 15, 17, 16, 16, 19, 18, 19, 20, 19, 18, 16, 11, 13, 19, 14, 12 };

我想对所有大于或等于17的元素求和。只有当一组元素的值大于或等于17时,才应该这样做。因此,如果有一个独立的元素具有一个值等于或大于17的值,例如位置1、16和28中的值,则应将其忽略并且不将其加到总和中。

应该有两个总和,预期结果应该是:

第一笔款项= 143 ([pos5] 17 + [pos6] 17 + [pos7] 18 + [pos8] 19 + [pos9] 20 + [pos10] 18 + [pos11] 17 + [pos12] 17)

第二笔和= 145 ([pos19] 19 + [pos20] 18 + [pos21] 19 + [pos22] 20 + [pos23] 19 + [pos24] 18)

预先感谢

我正在尝试使用一些布尔值来实现我希望达到的目标,但是这并没有按预期工作。我做了一个if语句,将所有大于或等于17的元素求和,但是当我希望程序产生和(在这种情况下)时,这只是将所有大于或等于17的元素求和。 2个和)大于或等于17且具有相邻元素也大于或等于17的元素。我也尝试了其他方法,但是未能产生预期的结果。< / p>

            if (stepIntoIf == false) {
                if (nums[i] >= 17 && nums[i + 1] < 17)
                {
                    stepIntoIf = true;
                    booleanChanger(stepIntoIf);
                    continue;
                }

            }


            if (nums[i] >= 17)
            {
                while(fisrtvisited == false)
                {
                    firstIndex = i;
                    fisrtvisited = true;
                }
                allTotal = nums[i] + allTotal;

                if (nums[i+1] < 17)
                {
                    while (secondVisited == false)
                    {
                        secondIndex = i;
                        secondVisited = true;
                    }

                }
            }

2 个答案:

答案 0 :(得分:0)

它不是很漂亮,但是可以用:

var values = new [] { 13, 18, 13, 12, 13, 17, 17, 18, 19, 20, 18, 17, 17, 12, 13, 15, 17, 16, 16, 19, 18, 19, 20, 19, 18, 16, 11, 13, 19, 14, 12 };

var extended = new [] { 0 }.Concat(values).Concat(new [] { 0 }).ToArray();

var results =
    Enumerable
        .Range(0, values.Length)
        .Select(x => values.Skip(x).Take(3).ToArray())
        .Where(x => x.Length == 3)
        .Select(x => x[1] >= 17 && (x[0] >= 17 || x[2] >= 17) ? x[1] : 0)
        .Aggregate(new List<List<int>>(), (a, x) =>
        {
            if (x == 0)
                a.Add(new List<int>());
            else
                a.Last().Add(x);
            return a;
        })
        .Where(x => x.Count > 0)
        .Select(x => x.Sum())
        .ToArray();

得到143113

答案 1 :(得分:0)

您可以使用扩展名GroupWhile合并> = 17的范围,然后将长度> 1的范围求和,例如:

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

namespace System.Collections.Generic
{
    public static class EnumerableExtensions
    {
        public static IEnumerable<IEnumerable<T>> GroupWhile<T>(
            this IEnumerable<T> source, Func<T, bool> predicate)
        {
            using (var iterator = source.GetEnumerator())
            {
                List<T> currentList = null;
                while (iterator.MoveNext())
                {
                    if (predicate(iterator.Current))
                    {
                        if (currentList == null)
                        {
                            currentList = new List<T>() { iterator.Current };
                        }
                        else
                        {
                            currentList.Add(iterator.Current);
                        }

                    }
                    else
                    {
                        if (currentList != null)
                        {
                            yield return currentList;
                            currentList = null;
                        }
                    }
                }
                if (currentList != null)
                {
                    yield return currentList;
                }
            }
        }
    }
}

namespace StackOverflow
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var values = new[] {
                13, 18, 13, 12, 13, 17, 17, 18, 19, 20, 18,
                17, 17, 12, 13, 15, 17, 16, 16, 19, 18, 19,
                20, 19, 18, 16, 11, 13, 19, 14, 12
            };

            var sums = values
                .AsEnumerable()
                .GroupWhile(item => item >= 17)
                .Select(range => range.ToArray())
                .Where(array => array.Length > 1)
                .Select(array => array.Sum());

            foreach (var sum in sums)
            {
                Console.WriteLine("Sum = {0}", sum);
            }
        }
    }
}

哪个输出:

Sum = 143
Sum = 113