lab5_function.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //Задача 1
  2. Console.WriteLine("Введите год рождения: ");
  3. int year1 = int.Parse(Console.ReadLine());
  4. for (int year = 1900; year < 2000; year++)
  5. {
  6. for (int month = 1; month <= 12; month++)
  7. {
  8. for (int day = 1; day <= DateTime.DaysInMonth(year, month); day++)
  9. {
  10. DateTime date = new DateTime(year, month, day);
  11. int age = year1 - year;
  12. int sum1 = CalculateSumOfSquares(year) - (day * day);
  13. if (sum1 == age)
  14. {
  15. Console.WriteLine("Дата рождения по свойству Васи: " + date.ToShortDateString());
  16. }
  17. }
  18. }
  19. }
  20. static int CalculateSumOfSquares(int year)
  21. {
  22. int sum = 0;
  23. foreach (char c in year.ToString())
  24. {
  25. sum += int.Parse(c.ToString()) * int.Parse(c.ToString());
  26. }
  27. return sum;
  28. }
  29. //Задача 2
  30. Console.WriteLine("Введите буковки:");
  31. string originalString = Console.ReadLine();
  32. string packedString = PackString(originalString);
  33. Console.WriteLine("Packed String: " + packedString);
  34. }
  35. static string PackString(string originalString)
  36. {
  37. if (string.IsNullOrEmpty(originalString))
  38. {
  39. return "";
  40. }
  41. string packed = "";
  42. int count = 1;
  43. for (int i = 0; i < originalString.Length; i++)
  44. {
  45. if (i == originalString.Length - 1 || originalString[i] != originalString[i + 1])
  46. {
  47. packed += count.ToString() + originalString[i];
  48. count = 1;
  49. }
  50. else
  51. {
  52. count++;
  53. }
  54. }
  55. return packed;
  56. }
  57. }
  58. //Задача 3
  59. Console.WriteLine("Введите цифры: ");
  60. int secret = Convert.ToInt16(Console.ReadLine());
  61. int guess = Convert.ToInt16(Console.ReadLine());
  62. Console.WriteLine($"Быки: {CountBulls(secret, guess)}");
  63. Console.WriteLine($"Коровы: {CountCows(secret, guess)}");
  64. static int CountBulls(int secret, int guess)
  65. {
  66. return secret.ToString().Zip(guess.ToString(), (s, g) => s == g ? 1 : 0).Sum();
  67. }
  68. static int CountCows(int secret, int guess)
  69. {
  70. return secret.ToString().Intersect(guess.ToString()).Count() - CountBulls(secret, guess);
  71. }