Exemple #1
0
        public static TargetNumbers Get2Numbers(List <int> numbers, int target)
        {
            var targetNumbers = new TargetNumbers();

            numbers.ForEach(x =>
            {
                var question = target - x;
                if (numbers.Contains(question))
                {
                    targetNumbers.ItemA = x;
                    targetNumbers.ItemB = question;
                }
                ;
            });
            return(targetNumbers);
        }
Exemple #2
0
        public static TargetNumbers Get3Numbers(List <int> numbers, int target)
        {
            var targetNumbers = new TargetNumbers();

            foreach (var item in numbers)
            {
                var newTarget  = target - item;
                var newNumbers = new List <int>(numbers);
                newNumbers.Remove(item);
                var twoTargetNumbers = Get2Numbers(newNumbers, newTarget);
                if (twoTargetNumbers.ItemA == 0 && twoTargetNumbers.ItemB == 0)
                {
                    continue;
                }
                targetNumbers.ItemA = twoTargetNumbers.ItemA;
                targetNumbers.ItemB = twoTargetNumbers.ItemB;
                targetNumbers.ItemC = item;
                break;
            }
            return(targetNumbers);
        }