Advertisement
dimitarbogdanov

рождена дата

Nov 26th, 2022
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. class Solution
  2. {
  3.     public static void Main()
  4.     {
  5.         int[] bdayDateArr = Console.ReadLine().Split(' ', 2).Select(Int32.Parse).ToArray();
  6.         int[] nowDateArr = Console.ReadLine().Split(' ', 3).Select(Int32.Parse).ToArray();
  7.        
  8.         int nowDate = nowDateArr[0];
  9.         int nowMonth = nowDateArr[1];
  10.         int nowYear = nowDateArr[2];
  11.         int bdayDate = bdayDateArr[0];
  12.         int bdayMonth = bdayDateArr[1];
  13.         int bdayYear = nowYear;
  14.        
  15.         if (bdayDate == 29
  16.          && bdayMonth == 2)
  17.         {
  18.             // calc next leap year
  19.             while (!(bdayYear % 4 == 0 || bdayYear % 100 != 0 && bdayYear % 4 == 0))
  20.             {
  21.                 bdayYear++;
  22.             }
  23.         }
  24.  
  25.         // Constraints
  26.         if (nowYear is < 1920 or > 3000
  27.             || nowMonth is < 1 or > 12
  28.             || nowDate < 1 || nowDate > DateTime.DaysInMonth(nowYear, nowMonth)
  29.             || bdayMonth is < 1 or > 12
  30.             || bdayDate < 1 || bdayDate > DateTime.DaysInMonth(bdayYear, bdayMonth))
  31.         {
  32.             return;
  33.         }
  34.        
  35.         DateTime now = new(nowYear, nowMonth, nowDate);
  36.         DateTime birthday = new(bdayYear, bdayMonth, bdayDate);
  37.         if (now > birthday)
  38.         {
  39.             birthday = birthday.AddYears(1);
  40.         }
  41.         int daysUntilBirthday = (birthday - now).Days;
  42.         Console.WriteLine(daysUntilBirthday);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement