No Description

ababin 6b8f14cb07 Обновить 'README.md' 2 months ago
.gitignore 061ad14601 lab 2 months ago
README.md 6b8f14cb07 Обновить 'README.md' 2 months ago

README.md

1.


while (true)
{
    Console.WriteLine("Введите число: ");
    int day = int.Parse(Console.ReadLine());
    Console.WriteLine("Введите месяц: ");
    int mon = int.Parse(Console.ReadLine());
    Console.WriteLine("Введите год: ");
    int year = int.Parse(Console.ReadLine());
    
    if ((day > 31 || day <= 0) || (mon > 12 || mon <= 0) || (year <= 0))
    {
        Console.WriteLine("Вышла фатальная ошибка \nПовторить? (Да, Нет)");
        string str = Console.ReadLine();
        if ((str == "Да") || (str == "да") || (str == "ДА") || (str == "дА"))
        {
            bool valid = true;
            for (int i = 0; i < 2; i++)
            {
                if (valid == true)
                {
                    break;
                }
            }
        }
        else if ((str == "Нет") || (str == "нет") || (str == "НЕТ") || (str == "нЕт") || (str == "неТ"))
        {
            break;
        }
        else
        {
            Console.WriteLine("Неверный ввод, повторите попытку");
            bool valid = true;
            for (int i = 0; i < 2; i++)
            {
                if (valid == true)
                {
                    break;
                }
            }
        }
        
    }
    else
    {
        Console.WriteLine($"Дата: {day}.{mon}.{year}");
    }
}

2.

class Program
{
    static void Main()
    {
        Console.WriteLine("Введите первую дату (в формате дд-мм-гггг): ");
        DateTime date1 = DateTime.Parse(Console.ReadLine());
        Console.WriteLine("Введите вторую дату (в формате дд-мм-гггг): ");
        DateTime date2 = DateTime.Parse(Console.ReadLine());
        int days = (date2 - date1).Days;
        Console.WriteLine($"Разница между датами составляет {days} дней.");
    }
}

3.

class Program
{
    static void Main()
    {
        Console.WriteLine("Введите первую дату (в формате чч:мм): ");
        DateTime date1 = DateTime.Parse(Console.ReadLine());
        Console.WriteLine("Введите вторую дату (в формате чч:мм): ");
        DateTime date2 = DateTime.Parse(Console.ReadLine());
        int hours = (int)(date2 - date1).TotalHours;
        Console.WriteLine($"Разница между датами составляет {hours} часов.");
    }
}

4.

class Program
{
    static void Main()
    {
        Console.WriteLine("Введите первую дату (в формате  чч:мм:сс): ");
        DateTime firstDate = DateTime.Parse(Console.ReadLine());
        Console.WriteLine("Введите вторую дату (в формате  чч:мм:сс): ");
        DateTime secondDate = DateTime.Parse(Console.ReadLine());
        int minutes = (int)(secondDate - firstDate).TotalMinutes;
        Console.WriteLine($"Разница во времени составляет {minutes} минут.");
    }
}

5.

class Program
{
    static void Main()
    {
Console.WriteLine("Введите год: ");
int year = int.Parse(Console.ReadLine());
int dayOfYear = 256;
DateTime programmersDay = new DateTime(year, 1, 1).AddDays(dayOfYear - 1);
Console.WriteLine($"День программиста в {year} году: {programmersDay.ToString("d MMMM yyyy")} ({programmersDay.DayOfWeek})");
    }
}

6.

class Program
{
    static void Main()
    {
Console.WriteLine("Введите номер дня года (1-365): ");
int dayOfYear = int.Parse(Console.ReadLine());
DateTime date = new DateTime(DateTime.Now.Year, 1, 1);
date = date.AddDays(dayOfYear - 1);
Console.WriteLine($"День недели: {date.DayOfWeek} ({(int)date.DayOfWeek})");
    }
}

7.

class Program
{
    static void Main()
    {
        DateTime examDate = new DateTime(2006, 5, 30); 
        Console.WriteLine("Введите текущую дату в формате год-месяц-день (например, 2006-02-21): ");
        DateTime currentDate = DateTime.Parse(Console.ReadLine());

        if (currentDate < examDate)
        {
            TimeSpan remainingDays = examDate - currentDate;
            Console.WriteLine($"Осталось {remainingDays.Days} дней до экзамена");
        }
        else if (currentDate > examDate)
        {
            TimeSpan passedDays = currentDate - examDate;
            Console.WriteLine($"Прошло {passedDays.Days} дней с момента экзамена");
        }
        else
        {
            Console.WriteLine("Сегодня экзамен!");
        }
    }
}

8.

class Program
{
    static void Main()
    {
DateTime tomorrow = DateTime.Now.AddDays(1);
string formattedDate = tomorrow.ToString("dd.MM.yyyy");
Console.WriteLine(formattedDate);
    }
} 

9.

class Program
{
    static void Main()
    {
        string birthday = "21.02"; 
        DateTime currentDate = DateTime.Today;
        int day = int.Parse(birthday.Split('.')[0]);
        int month = int.Parse(birthday.Split('.')[1]);
        DateTime currentBirthday = new DateTime(currentDate.Year, month, day);
        if (currentDate.Day == day && currentDate.Month == month)
        {
            Console.WriteLine("С днем рождения!");
        }
        else if (currentDate < currentBirthday)
        {
            int daysLeft = (currentBirthday - currentDate).Days;
            Console.WriteLine("До дня рождения осталось: {0} дней", daysLeft);
        }
        else
        {
            var nextBirthday = currentBirthday.AddYears(1);
            int daysPassed = (currentDate - currentBirthday).Days;
            Console.WriteLine("Дней с дня рождения прошло: {0} дней", daysPassed);
        }
    }
}

10.

class Program
{
    static void Main()
    {
        Console.Write("Введите время в формате HH:MM:SS: ");
        string time = Console.ReadLine();
        string[] timeArray = time.Split(':');
        int hours = Convert.ToInt32(timeArray[0]);
        int minutes = Convert.ToInt32(timeArray[1]);
        int seconds = Convert.ToInt32(timeArray[2]);
        int totalSeconds = hours * 3600 + minutes * 60 + seconds;
        Console.WriteLine($"Прошло секунд с начала суток: {totalSeconds}");
    }
}

11.

class Program
{
    static void Main()
    {
        List<DateTime> dates = new List<DateTime>
        {
            new DateTime(2023, 5, 15),
            new DateTime(2022, 6, 20),
            new DateTime(2021, 7, 5),
            new DateTime(2020, 8, 10)
        };
        DateTime nearestDate = FindNearestDate(dates);

        Console.WriteLine($"Ближайшая дата: {nearestDate.ToString("dd.MM.yyyy")}");
    }
    static DateTime FindNearestDate(List<DateTime> dates)
    {
        DateTime currentDate = DateTime.Today;
        DateTime nearestDate = dates[0];

        foreach (DateTime date in dates)
        {
            if (Math.Abs((date - currentDate).TotalDays) < Math.Abs((nearestDate - currentDate).TotalDays))
            {
                nearestDate = date;
            }
        }
        return nearestDate;
    }
}

12.

namespace BirthdaysByMonthSimple
{
    class Program
    {
        static void Main(string[] args)
        {
            List<DateTime> birthdays = new List<DateTime>
            {
                DateTime.Parse("01.01.2023"),
                DateTime.Parse("15.02.2023"),
                DateTime.Parse("10.03.2023"),
                DateTime.Parse("05.04.2023"),
                DateTime.Parse("20.05.2023"),
                DateTime.Parse("01.06.2023"),
                DateTime.Parse("15.07.2023"),
                DateTime.Parse("10.08.2023"),
                DateTime.Parse("05.09.2023"),
                DateTime.Parse("20.10.2023"),
                DateTime.Parse("01.11.2023"),
                DateTime.Parse("15.12.2023"),
            };
            var birthdaysByMonth = birthdays.GroupBy(d => d.Month)
                .Select(g => new { Month = g.Key, Count = g.Count() })
                .OrderBy(g => g.Month);
            foreach (var group in birthdaysByMonth)
            {
                Console.WriteLine($"{group.Month}: {group.Count}");
            }
        }
    }
}

13.

class Program
{
    static void Main()
    {
        string[] dates = {"01.05.2022", "15.06.2023", "20.05.2022", "10.07.2022", "05.06.2023", "18.05.2022"};
        Dictionary<string, int> monthsCount = new Dictionary<string, int>();
        foreach (var date in dates)
        {
            string[] parts = date.Split('.');
            string month = parts[1];
if (monthsCount.ContainsKey(month))
            {
                monthsCount[month]++;
            }
            else
            {
                monthsCount[month] = 1;
            }
        }
        string mostPopularMonth = monthsCount.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;

        Console.WriteLine("Самый популярный месяц: " + mostPopularMonth);
    }
}

14.

class Program
{
    static void Main()
    {
        int fridayCount = 0;

        for (int i = 1; i <= 12; i++)
        {
            DateTime date = new DateTime(2022, i, 13);
            if (date.DayOfWeek == DayOfWeek.Friday)
            {
                fridayCount++;
            }
        }

        double totalDays = 365;
        double fridayProbability = fridayCount / totalDays;

        Console.WriteLine("Вероятность выпадения 13 числа на пятницу: " + fridayProbability);
        Console.WriteLine("Вероятности выпадения 13 числа на каждый день недели:");

        for (int j = 0; j < 7; j++)
        {
            int dayCount = 0;
            for (int i = 1; i <= 12; i++)
            {
                DateTime date = new DateTime(2022, i, 13);
                if ((int)date.DayOfWeek == j)
                {
                    dayCount++;
                }
            }
            double dayProbability = dayCount / totalDays;
            Console.WriteLine(((DayOfWeek)j).ToString() + ": " + dayProbability);
        }
    }
}

15.

class Program
{
    static void Main()
    {
        string input = "00:00-01:00,00:30-02:30,02:00-03:30"; 
        string[] schedules = input.Split(',');

        Dictionary<int, int> scheduleCount = new Dictionary<int, int>();
        foreach (string schedule in schedules)
        {
            string[] times = schedule.Split('-');
            string startTime = times[0];
            string[] startComponents = startTime.Split(':');
            int startHour = int.Parse(startComponents[0]);
            string endTime = times[1];
            string[] endComponents = endTime.Split(':');
            int endHour = int.Parse(endComponents[0]);
            for (int i = startHour; i <= endHour; i++)
            {
                if (scheduleCount.ContainsKey(i))
                {
                    scheduleCount[i]++;
                }
                else
                {
                    scheduleCount[i] = 1;
                }
            }
        }
        int maxCount = 0;
        int maxHour = 0;
        foreach (var pair in scheduleCount)
        {
            if (pair.Value > maxCount)
            {
                maxCount = pair.Value;
                maxHour = pair.Key;
            }
        }
        Console.WriteLine($"Наибольшее количество касс ({maxCount}) работает в промежуток времени с {maxHour}:00 до {maxHour + 1}:00.");
    }

}

16.

class Program
{
    static void Main()
    {
        int year = 2023;

        int weekendsCount = CountWeekends(year);

        Console.WriteLine($"Количество выходных дней в {year} году: {weekendsCount}");
    }
    static int CountWeekends(int year)
    {
        int weekendsCount = 0;

        for (int month = 1; month <= 12; month++)
        {
            int daysInMonth = DateTime.DaysInMonth(year, month);

            for (int day = 1; day <= daysInMonth; day++)
            {
                DateTime date = new DateTime(year, month, day);

                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {
                    weekendsCount++;
                }
            }
        }
        return weekendsCount;
    }
}