C# :: Aufgabe #342

3 Lösungen Lösungen öffentlich

Zielwertsuche in einem sortierten Array

Fortgeschrittener - C# von JKooP - 21.11.2020 um 09:10 Uhr
Aufgabe 1 (leicht)
In einem beliebig großen sortieren Array soll das erste Vorkommen desjenigen Zahlenpaares per Index ausgegeben werden, bei dem die Summe dem gesuchten Zielwert entspricht.
Dabei kommt jede Zahl nur einmal im Array vor.

arr = [3, 4, 15, 22]
target = 7
Index = [0, 1] (3+4)

arr = [1, 3, 4, 6]
target = 8
Index = [0, 0] oder NULL/Nothing (keine Lösung)

Aufgabe 2 (mittel)
Wie Aufgabe 1, jedoch können Zahlen doppelt vorkommen.

arr = [4, 4, 5, 6]
target = 8
Index = [0, 1] (4+4)

Aufagbe 3 (schwer)
In einem beliebig großen sortieren Array sollen alle Vorkommen derjenigen Zahlenpaare per Indizes ausgegeben werden, bei denen die Summen dem gesuchten Zielwert entsprechen. Dabei können alle Zahlen mehrfach vorkommen.

arr = [1, 3, 4, 6]
target = 7
Indizes = [0, 3] (1+6), [1, 2] (3+4)

Noch ein wenig schwieriger:
arr = [4, 4, 4, 5, 6]
target = 8
Indizes = [0, 1] (4+4), [0, 2] (4 + 4), [1, 2] (4 + 4)

Für alle Aufgaben gilt:
Dopplungen wie [1, 3] und [3, 1] gelten als gleichwertig und sollen nicht ausgegeben werden.

Viel Spaß

Lösungen:

vote_ok
von Dyvd (120 Punkte) - 02.12.2020 um 13:54 Uhr
(Aufgabe 2:)

Vorausgesetzt ist ein sortierter Array, ansonsten kann man mit Array.Sort eine Sortierung in die Funktion einbauen.

Quellcode ausblenden C#-Code
static string searchNumPair(int[] intArray, int target)
{
	 string numPairIndex = null;
	 int currentAddition;
	 string numOne, numTwo;
	 bool skip = false;

	 for (int i = 0; i < intArray.Length; i++)
	 {
			for (int x = 0; x < intArray.Length; x++)
			{
				
				if (i == x)
				{
					skip = true;
				}
				else
				{
					skip = false;
				}

				 currentAddition = intArray[i] + intArray[x];

				 if (currentAddition == target && !skip)
				 {
						numOne = Convert.ToString(i);
						numTwo = Convert.ToString(x);
						numPairIndex = $"[{numOne},{numTwo}]";
						goto Finished;
				 }
			}

	 }

	 //Finished:
	 if (numPairIndex == null)
	 {
      numPairIndex =[0, 0]
                                 //Oder 
			numPairIndex = "Cannot find Index of suitable numbers!";
	 }
	 
		 return numPairIndex;	
}
vote_ok
von schulle (50 Punkte) - 21.12.2020 um 19:17 Uhr
Quellcode ausblenden C#-Code


using System;

namespace Lesson342_Array
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] arr = new int[] { 4, 4, 4, 5, 6 };
            int target = 8;
            int summand1;
            int summand2;
            int summe;

            for (int i = 0; i < arr.Length; i++)
            {
                summand1 = arr[i];
                
                for (int j = i + 1; j < arr.Length; j++)
                {
                    summand2 = arr[j];
                    summe = summand1 + summand2;
                    if (summe  == target)
                    {
                        Console.WriteLine("Target getroffen, {0} + {1} = {2}: index ist: [{3},{4}]", summand1, summand2, summe,i,j);
                        //break; wenn beim ersten Treffer die Prüfungen enden soll 
                    }
                    else
                    {
                        Console.WriteLine("leider nein");
                    }
                }
               
            }

            

        }
    }
}

1 Kommentar
vote_ok
von Frank (410 Punkte) - 23.02.2021 um 20:01 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;

namespace Zielwertsuche
{
    static class Program
    {
        static void Main()
        {
            int[] list = new int[] { 1, 5, 3, 8, 2, 5, 1, 7, 4, 6, 3, 9, 6, 8, 1, 5, 2 };
            int target = 6;

            List<Tuple<int, int>> pairs = GetListOfPairs(list, target);

            foreach (Tuple<int, int> pair in pairs)
            {
                Console.WriteLine($"indices: {pair.Item1} + {pair.Item2}, Values: {list[pair.Item1]} + {list[pair.Item2]}");
            }
            Console.WriteLine($"number: {pairs.Count}");
        }

        private static List<Tuple<int, int>> GetListOfPairs(int[] list, int target)
        {
            List<Tuple<int, int>> pairs = new List<Tuple<int, int>>();
            for (int i = 0; i < list.Length; i++)
            {
                for (int j = 1; j < list.Length; j++)
                {
                    if (i == j || i < j) break;
                    if (list[i] + list[j] == target)
                    {
                        pairs.Add(new Tuple<int, int>(j, i));
                    }
                }
            }
            return pairs;
        }
    }
}