Guest User

Untitled

a guest
May 24th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. namespace Problem04.MagicDates
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. internal class Program
  8. {
  9. private static void Main(string[] args)
  10. {
  11. int startYear = int.Parse(Console.ReadLine());
  12. int endYear = int.Parse(Console.ReadLine());
  13. var magicDates = new List<string>();
  14. int weight = int.Parse(Console.ReadLine());
  15.  
  16. DateTime startDate = new DateTime(startYear, 1, 1);
  17. DateTime endDate = new DateTime(endYear, 12, 31);
  18. List<int> digits = new List<int>(8);
  19. for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
  20. {
  21. digits.Clear();
  22. int days = date.Day;
  23. while (days > 0)
  24. {
  25. digits.Add(days % 10);
  26. days /= 10;
  27. }
  28.  
  29. int month = date.Month;
  30. while (month > 0)
  31. {
  32. digits.Add(month % 10);
  33. month /= 10;
  34. }
  35.  
  36. int year = date.Year;
  37. while (year > 0)
  38. {
  39. digits.Add(year % 10);
  40. year /= 10;
  41. }
  42.  
  43. digits = digits.Where(x => x != 0).ToList();
  44. int currentWeight = 0;
  45. for (int i = 0; i < digits.Count - 1; i++)
  46. {
  47. for (int j = i + 1; j < digits.Count; j++)
  48. {
  49. currentWeight += digits[i] * digits[j];
  50. }
  51. }
  52.  
  53. if (currentWeight == weight)
  54. {
  55. magicDates.Add(date.ToString("dd-MM-yyyy"));
  56. }
  57. }
  58.  
  59. if (magicDates.Count == 0)
  60. {
  61. Console.WriteLine("No");
  62. return;
  63. }
  64.  
  65. Console.WriteLine(string.Join(Environment.NewLine, magicDates));
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment