Чернов Данил И-21

dpokatski da1c3eed1e lab7 10 months ago
lab1 1c21483e31 lab6 10 months ago
lab2 030b5c7a7d lab2 11 months ago
lab3 b095883763 lab3 11 months ago
lab4 ae1ca97f06 lab4 11 months ago
lab5 80dcdc3d8c lab5 11 months ago
lab6 1c21483e31 lab6 10 months ago
lab7 da1c3eed1e lab7 10 months ago
readme.md da1c3eed1e lab7 10 months ago

readme.md

Lab 7. Функции (методы)

Task 1

solution:

Console.WriteLine("Введите год рождения: ");
int year1 = int.Parse(Console.ReadLine());

for (int year = 1900; year < 2000; year++)
{
    for (int month = 1; month <= 12; month++)
    {
        for (int day = 1; day <= DateTime.DaysInMonth(year, month); day++)
        {
            DateTime date = new DateTime(year, month, day);
            int age = year1 - year;
            int sum1 = CalculateSumOfSquares(year) - (day * day);

            if (sum1 == age)
            {
                Console.WriteLine("Дата рождения по свойству Васи: " + date.ToShortDateString());
            }
        }
    }
}

static int CalculateSumOfSquares(int year)
{
    int sum = 0;
    foreach (char c in year.ToString())
    {
        sum += int.Parse(c.ToString()) * int.Parse(c.ToString());
    }
    return sum;
}

Task 2

solution:

static void Main(string[] args)
{
    Console.WriteLine("Введите значение:");
    string originalString = Console.ReadLine();

    string packedString = PackString(originalString);
    Console.WriteLine("Packed String: " + packedString);
}

static string PackString(string originalString)
{
    if (string.IsNullOrEmpty(originalString))
    {
        return "";
    }

    string packed = "";
    int count = 1;

    for (int i = 0; i < originalString.Length; i++)
    {
        if (i == originalString.Length - 1 || originalString[i] != originalString[i + 1])
        {
            packed += count.ToString() + originalString[i];
            count = 1;
        }
        else
        {
            count++;
        }
    }

    return packed;
}

Task 3

solution:

Console.WriteLine("Введите цифры: ");
int secret = Convert.ToInt16(Console.ReadLine());
int guess = Convert.ToInt16(Console.ReadLine());

Console.WriteLine($"Быки: {CountBulls(secret, guess)}");
Console.WriteLine($"Коровы: {CountCows(secret, guess)}");

static int CountBulls(int secret, int guess)
{
    return secret.ToString().Zip(guess.ToString(), (s, g) => s == g ? 1 : 0).Sum();
}

static int CountCows(int secret, int guess)
{
    return secret.ToString().Intersect(guess.ToString()).Count() - CountBulls(secret, guess);
}