Alexander7337

SumOfDateDigits

Jan 16th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2.  
  3. public class MagicDates
  4. {
  5.     public static void Main()
  6.     {
  7.         int startYear = int.Parse(Console.ReadLine());
  8.         int endYear = int.Parse(Console.ReadLine());
  9.         int magicWeight = int.Parse(Console.ReadLine());
  10.  
  11.         DateTime startDate = new DateTime(startYear, 1, 1);
  12.         DateTime endDate = new DateTime(endYear, 12, 31);
  13.         int count = 0;
  14.         for (DateTime d = startDate; d <= endDate; d = d.AddDays(1))
  15.         {
  16.             int d1 = d.Day / 10;
  17.             int d2 = d.Day % 10;
  18.             int d3 = d.Month / 10;
  19.             int d4 = d.Month % 10;
  20.             int d5 = (d.Year / 1000) % 10;
  21.             int d6 = (d.Year / 100) % 10;
  22.             int d7 = (d.Year / 10) % 10;
  23.             int d8 = (d.Year / 1) % 10;
  24.             int[] digits = { d1, d2, d3, d4, d5, d6, d7, d8 };
  25.             int weight = 0;
  26.             for (int first = 0; first < digits.Length; first++)
  27.             {
  28.                 for (int second = first + 1; second < digits.Length; second++)
  29.                 {
  30.                     weight = weight + (digits[first] * digits[second]);
  31.                 }
  32.             }
  33.             //Console.WriteLine("{0:d2}-{1:d2}-{2:d2} weight={3}", d.Day, d.Month, d.Year, weight);
  34.             if (weight == magicWeight)
  35.             {
  36.                 Console.WriteLine("{0:d2}-{1:d2}-{2:d2}", d.Day, d.Month, d.Year);
  37.                 count++;
  38.             }
  39.         }
  40.  
  41.         if (count == 0)
  42.         {
  43.             Console.WriteLine("No");
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment