Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. namespace Problem_07.Equality_Logic
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7.  
  8. public class Program
  9. {
  10. private static void Main(string[] args)
  11. {
  12. var listOfSortedPeople= new SortedSet<Person>();
  13. var listOfHashedPeople = new HashSet<Person>();
  14. var countPeople = int.Parse(Console.ReadLine());
  15. for (var i = 0; i < countPeople; i++)
  16. {
  17. var input = Console.ReadLine().Split();
  18. var name = input[0];
  19. var age = int.Parse(input[1]);
  20. var person = new Person(name, age);
  21. listOfHashedPeople.Add(person);
  22. listOfSortedPeople.Add(person);
  23. }
  24.  
  25. Console.WriteLine(listOfHashedPeople.Count);
  26. Console.WriteLine(listOfSortedPeople.Count);
  27. }
  28. }
  29.  
  30. public class Person : IComparable<Person>
  31. {
  32. public Person(string name, int age)
  33. {
  34. this.Name = name;
  35. this.Age = age;
  36. }
  37.  
  38. public string Name { get; }
  39.  
  40. public int Age { get; }
  41. public override bool Equals(object other)
  42. {
  43. var y = other as Person;
  44. return this.Name == y.Name && this.Age == y.Age;
  45. }
  46.  
  47. public override int GetHashCode()
  48. {
  49. var hashCode = this.Name.Length * 10000;
  50. foreach (var letter in this.Name)
  51. {
  52. hashCode += letter;
  53. }
  54.  
  55. hashCode = this.Name.Aggregate(hashCode, (current, letter) => current + letter);
  56. hashCode += this.Age * 10000000;
  57.  
  58. return hashCode;
  59. }
  60.  
  61. public int CompareTo(Person other)
  62. {
  63. var result = this.Name.CompareTo(other.Name);
  64. if (result == 0)
  65. {
  66. result = this.Age - other.Age;
  67. }
  68.  
  69. return result;
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement