Advertisement
Guest User

Untitled

a guest
Apr 14th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Threading;
  5.  
  6. namespace _04.MagicDates
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
  13.             int startYear = int.Parse(Console.ReadLine());
  14.             int endYear = int.Parse(Console.ReadLine());
  15.             int magicWeight = int.Parse(Console.ReadLine());
  16.  
  17.             List<DateTime> dates = new List<DateTime>();
  18.             DateTime startDate = new DateTime(startYear, 1, 1);
  19.             DateTime endDate = new DateTime(endYear, 12, 31);
  20.  
  21.             int result = 0;
  22.             int mult = 0;
  23.             for (DateTime currentDate = startDate; currentDate <= endDate; currentDate = currentDate.AddDays(1))
  24.             {
  25.                 result = 0;
  26.                 string date = currentDate.ToString("dd-MM-yyyy");
  27.                 date = date.Replace(":", "").Replace("-", "").TrimStart('0');
  28.                 int number = int.Parse(date);
  29.                 for (int i = 0, m = (int)Math.Pow(10, date.Length - 1); i < date.Length; i++, m /= 10)
  30.                 {
  31.                     mult = number / m % 10;
  32.                     for (int j = i + 1, n = m / 10; j < date.Length; j++, n /= 10)
  33.                     {
  34.                         result += mult * (number / n % 10);
  35.                     }
  36.                 }
  37.                 if (result == magicWeight)
  38.                 {
  39.                     dates.Add(currentDate);
  40.                 }
  41.             }
  42.             if (dates.Count > 0)
  43.             {
  44.                 foreach (var date in dates)
  45.                 {
  46.                     Console.WriteLine(date.ToString("dd-MM-yyyy"));
  47.                 }
  48.             }
  49.             else
  50.             {
  51.                 Console.WriteLine("No");
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement