Задача 5 на множества из учебника
SortedSet<int> one = new SortedSet<int>();
SortedSet<int> two = new SortedSet<int>();
SortedSet<int> three = new SortedSet<int>();
Console.Write("Числа в множестве(от и до): ");
string[] interval = Console.ReadLine().Split(" ");
for (int i = Convert.ToInt32(interval[0]); i <= Convert.ToInt32(interval[1]); i++)
{
one.Add(i);
}
foreach (var a in one)
{
if (IsSimple(a))
{
two.Add(a);
}
else
{
three.Add(a);
}
}
ShowColl(two,"Составные числа: ");
ShowColl(three,"Простые числа: ");
static bool IsSimple(int a)
{
int b = Convert.ToInt32(Math.Sqrt(a));
for (int i = 2; i <= b; i++)
{
if (a % i == 0)
{
return false;
}
}
return true;
}
static void ShowColl(SortedSet<int> numbers, string a)
{
Console.Write(a);
foreach (var b in numbers)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
1
while (true)
{
Console.Write("date(dd.mm.yyyy): ");
try
{
DateTime date = Convert.ToDateTime(Console.ReadLine());
Console.WriteLine("date is correct");
}
catch (Exception)
{
Console.WriteLine("wrong date");
}
}
2
while (true)
{
Console.Write("first date: ");
DateTime date1 = Convert.ToDateTime(Console.ReadLine());
Console.Write("second date: ");
DateTime date2 = Convert.ToDateTime(Console.ReadLine());
Console.WriteLine("days: {0}", date1.Subtract(date2).TotalDays);
}
3
while (true)
{
Console.Write("first date: ");
DateTime date1 = Convert.ToDateTime(Console.ReadLine());
Console.Write("second date: ");
DateTime date2 = Convert.ToDateTime(Console.ReadLine());
Console.WriteLine("hours: {0}", date1.Subtract(date2).TotalHours);
}
4
while (true)
{
Console.Write("first date: ");
DateTime date1 = Convert.ToDateTime(Console.ReadLine());
Console.Write("second date: ");
DateTime date2 = Convert.ToDateTime(Console.ReadLine());
Console.WriteLine("minutes: {0}", date1.Subtract(date2).TotalMinutes);
}
5
while (true)
{
Console.Write("year: ");
int year = int.Parse(Console.ReadLine());
DateTime date1 = new DateTime(year, 01, 01);
double day = 256 - date1.DayOfYear;
date1 = date1.AddDays(day);
Console.WriteLine("date: {0} day of week: {1}", date1.ToShortDateString(), date1.DayOfWeek);
}
6
while (true)
{
DateTime date1 = DateTime.Now;
Console.Write("day: ");
double day = Convert.ToDouble(Console.ReadLine());
if (day > date1.DayOfYear)
date1 = date1.AddDays(day - date1.DayOfYear);
else
date1 = date1.AddDays(-(date1.DayOfYear - day));
Console.WriteLine("day of week: {0}", date1.DayOfWeek);
}
7
while (true)
{
DateTime date1 = new DateTime(2024, 5, 23);
Console.Write("Дата: ");
DateTime date2 = Convert.ToDateTime(Console.ReadLine());
if (date1 > date2)
Console.WriteLine("До экзамена: {0} дней",date1.Subtract(date2).TotalDays);
else if (date1 < date2)
Console.WriteLine("Со дня экзамена прошло: {0} дней", date2.Subtract(date1).TotalDays);
else
Console.WriteLine("Экзамен сегодня!");
}
8
DateTime date = DateTime.Now;
Console.WriteLine(date.AddDays(1).ToShortDateString());
9
while(true)
{
Console.Write("День рождения(дд.мм.гггг): ");
string[] ddmm = Console.ReadLine().Split('.');
DateTime dob = new DateTime(2024, int.Parse(ddmm[1]), int.Parse(ddmm[0]));
DateTime date = DateTime.Now;
if (dob > date)
Console.WriteLine("До дня рождения {0} дней", Convert.ToInt32(dob.Subtract(date).TotalDays) );
else if (dob < date)
Console.WriteLine("Со дня рождения прошло {0} дней", Convert.ToInt32(date.Subtract(dob).TotalDays));
else
Console.WriteLine("С днем рождения!");
}
10
Console.Write("Введите время на часах: ");
string[] time = Console.ReadLine().Split(':');
DateTime date1 = DateTime.Today;
date1 = date1.AddHours(double.Parse(time[0])).AddMinutes(double.Parse(time[1])).AddSeconds(double.Parse(time[2]));
Console.WriteLine("С начала суток прошло: {0} секунд",Convert.ToInt32(date1.Subtract(DateTime.Today).TotalSeconds));
11
Console.Write("Введите количество дат: ");
int a = int.Parse(Console.ReadLine());
List<DateTime> dates = new List<DateTime>();
DateTime today = DateTime.Today;
for (int i = 0; i < a; i++)
{
Console.Write("Введите {0} дату: ", i+1);
dates.Add(Convert.ToDateTime(Console.ReadLine()));
}
double difference = Math.Abs(today.Subtract(dates[0]).TotalDays);
DateTime datean = new DateTime();
foreach (var date in dates)
{
double days = Math.Abs(today.Subtract(date).TotalDays);
if (days < difference)
{
difference = days;
datean = date;
}
}
Console.WriteLine("Ближайшая дата: {0}",datean.ToShortDateString());
12
Console.Write("Введите количество дат: ");
int a = int.Parse(Console.ReadLine());
List<DateTime> dates = new List<DateTime>();
DateTime today = DateTime.Today;
for (int i = 0; i < a; i++)
{
Console.Write("Введите {0} дату дня рождения: ", i+1);
dates.Add(Convert.ToDateTime(Console.ReadLine()));
}
List<int> months = new List<int>();
foreach (var date in dates)
{
if (months.IndexOf(date.Month) == -1)
{
months.Add(date.Month);
}
}
foreach (var month in months)
{
int count = 0;
Console.Write("Дней рождений в {0} месяце: ", month);
foreach (var date in dates)
{
if (date.Month == month)
{
count++;
}
}
Console.Write(count);
Console.WriteLine();
}
13
string [] dates = {"12.02.2005", "13.05.2005", "15.09.2014", "23.02.2019", "19.02.2001","30.09.2010"};
Dictionary<int, int> months = new Dictionary<int, int>();
foreach (var date in dates)
{
DateTime datec = DateTime.Parse(date);
if (months.ContainsKey(datec.Month))
{
months[datec.Month]++;
}
else
{
months.Add(datec.Month, 1);
}
}
int a = 0;
int b = 0;
foreach (var month in months)
{
if (month.Value > a)
{
a = month.Value;
b = month.Key;
}
}
Console.WriteLine($"{b} месяц самый популярный.");
14
Console.WriteLine("Понедельник: " + (double)Counter(DayOfWeek.Monday) / 50*12);
Console.WriteLine("Вторник: " + (double)Counter(DayOfWeek.Tuesday) / 50*12);
Console.WriteLine("Среда: " + (double)Counter(DayOfWeek.Wednesday) / 50*12);
Console.WriteLine("Четверг: " + (double)Counter(DayOfWeek.Thursday) / 50*12);
Console.WriteLine("Пятница: " + (double)Counter(DayOfWeek.Friday) / 50*12);
Console.WriteLine("Суббота: " + (double)Counter(DayOfWeek.Saturday) / 50*12);
Console.WriteLine("Воскресенье: " + (double)Counter(DayOfWeek.Sunday) / 50*12);
static int Counter(DayOfWeek day)
{
int count = 0;
for (int year = 1; year <= 50; year++)
{
for (int month = 1; month < 12; month++)
{
DateTime date = new DateTime(year, month, 13);
if (date.DayOfWeek == day)
{
count++;
}
}
}
return count;
}
15
Console.Write("year: ");
int year = int.Parse(Console.ReadLine());
DateTime date1 = new DateTime(year, 1, 1);
DateTime date2 = new DateTime(year, 12, 31);
Console.WriteLine(holidays(date1,date2));
static int holidays (DateTime date1, DateTime date2)
{
int days = 0;
for (var a = date1; a < date2; a.AddDays(1))
{
if (a.DayOfWeek == DayOfWeek.Saturday || a.DayOfWeek == DayOfWeek.Sunday)
{
days++;
}
}
return days;
}