Menu
Coddy logo textTech

Matematyka — różnica zbiorów

Część sekcji Logika i sterowanie przepływem ścieżki C# w Coddy. Lekcja 64 z 66.

Operacja różnicy zbiorów tworzy nowy zbiór zawierający elementy, które występują w pierwszym zbiorze, ale nie w drugim.

Utwórz dwa zbiory HashSet:

HashSet<string> set1 = new HashSet<string>();
HashSet<string> set2 = new HashSet<string>();

Dodaj elementy do obu zbiorów:

set1.Add("Apple");
set1.Add("Banana");
set1.Add("Cherry");

set2.Add("Banana");
set2.Add("Kiwi");

Oblicz różnicę (elementy w set1, ale nie w set2):

HashSet<string> difference = new HashSet<string>(set1);
difference.ExceptWith(set2);

Po wykonaniu powyższego kodu zbiór różnicy zawiera:

["Apple", "Cherry"]
challenge icon

Wyzwanie

Łatwy

Utwórz metodę o nazwie GetSetDifference, która przyjmuje dwa zbiory HashSet liczb całkowitych (set1 i set2) i zwraca nowy zbiór HashSet zawierający różnicę zbiorów (elementy z set1, których nie ma w set2).

Spróbuj swoich sił

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

class Program {
    public static HashSet<int> GetSetDifference(HashSet<int> set1, HashSet<int> set2) {
        // Wpisz tutaj swój kod
        return null;
    }
    
    static void Main(string[] args) {
        // Wczytaj pierwszy wiersz dla pierwszego zbioru
        string line1 = Console.ReadLine();
        HashSet<int> set1 = new HashSet<int>();
        
        // Sprawdź, czy pierwszy zbiór ma format tablicy JSON
        if (line1 != null && line1.StartsWith("[") && line1.EndsWith("]")) {
            try {
                // Wyodrębnij zawartość między nawiasami kwadratowymi
                string arrayContent = line1.Substring(1, line1.Length - 2);
                
                // Spróbuj sparsować zawartość tablicy JSON
                string[] values = Regex.Split(arrayContent, @",\s*");
                foreach (string value in values) {
                    // Usuń cudzysłowy, jeśli występują
                    string cleanValue = value.Trim();
                    if (cleanValue.StartsWith("\"") && cleanValue.EndsWith("\"")) {
                        cleanValue = cleanValue.Substring(1, cleanValue.Length - 2);
                    }
                    
                    // Przekształć na liczbę całkowitą i dodaj do zbioru
                    if (int.TryParse(cleanValue, out int num)) {
                        set1.Add(num);
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine($"Error parsing first set: {ex.Message}");
                return;
            }
        }
        else {
            // Przetwórz tradycyjne dane wejściowe oddzielone spacjami dla pierwszego zbioru
            string[] set1Input = line1.Split(' ');
            foreach (string item in set1Input) {
                if (int.TryParse(item, out int num)) {
                    set1.Add(num);
                }
            }
        }
        
        // Wczytaj drugi wiersz dla drugiego zbioru
        string line2 = Console.ReadLine();
        HashSet<int> set2 = new HashSet<int>();
        
        // Sprawdź, czy drugi zbiór ma format tablicy JSON
        if (line2 != null && line2.StartsWith("[") && line2.EndsWith("]")) {
            try {
                // Wyodrębnij zawartość między nawiasami kwadratowymi
                string arrayContent = line2.Substring(1, line2.Length - 2);
                
                // Spróbuj sparsować zawartość tablicy JSON
                string[] values = Regex.Split(arrayContent, @",\s*");
                foreach (string value in values) {
                    // Usuń cudzysłowy, jeśli występują
                    string cleanValue = value.Trim();
                    if (cleanValue.StartsWith("\"") && cleanValue.EndsWith("\"")) {
                        cleanValue = cleanValue.Substring(1, cleanValue.Length - 2);
                    }
                    
                    // Przekształć na liczbę całkowitą i dodaj do zbioru
                    if (int.TryParse(cleanValue, out int num)) {
                        set2.Add(num);
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine($"Error parsing second set: {ex.Message}");
                return;
            }
        }
        else {
            // Przetwórz tradycyjne dane wejściowe oddzielone spacjami dla drugiego zbioru
            string[] set2Input = line2.Split(' ');
            foreach (string item in set2Input) {
                if (int.TryParse(item, out int num)) {
                    set2.Add(num);
                }
            }
        }
        
        HashSet<int> difference = GetSetDifference(set1, set2);
        
        // Wypisz wynik
        List<int> sortedResult = new List<int>(difference);
        sortedResult.Sort();
        foreach (int item in sortedResult) {
            Console.WriteLine(item);
        }
    }
}
quiz iconSprawdź się

Ta lekcja zawiera krótki quiz. Zacznij lekcję, żeby na niego odpowiedzieć i śledzić swoje postępy.

Wszystkie lekcje w sekcji Logika i sterowanie przepływem

Poćwicz samodzielnie: Kompilator C# online