Advertisement
Levi0227

2. félév 11. hét

Apr 23rd, 2024
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     internal class Program
  10.     {
  11.         private static Random random = new Random();
  12.         static void Main(string[] args)
  13.         {
  14.             List<Person> people = new List<Person>();
  15.             for (int i = 0; i < 10; ++i)
  16.             {
  17.                 people.Add(new Person($"Person{i}", (i + 1) * 10, random.Next(200, 1501)));
  18.             }
  19.             //people[4] = people[0];
  20.  
  21.             bool azE = HalmazE(people);
  22.             Console.WriteLine(azE);
  23.            
  24.             bool tartE = TartalmazE(people, people[0]);
  25.             Console.WriteLine(tartE);
  26.            
  27.             bool tartENemxD = TartalmazE(people, new Person("xy", 1, 1));
  28.             Console.WriteLine(tartENemxD);
  29.  
  30.             bool reszE = ReszhalmazE(people, new List<Person>() { people[0], people[1], people[2] });
  31.             Console.WriteLine(reszE);
  32.  
  33.             Console.Read();
  34.         }
  35.  
  36.         private static bool HalmazE(List<Person> people)
  37.         {
  38.             int i = 1;
  39.             while (i < people.Count && people[i] != people[i-1])
  40.             {
  41.                 ++i;
  42.             }
  43.  
  44.             return i == people.Count;
  45.         }
  46.  
  47.         private static bool TartalmazE(List<Person> people, Person person)
  48.         {
  49.             int bal = 0;
  50.             int jobb = people.Count-1;
  51.             int center = (bal+jobb) / 2;
  52.  
  53.             while (bal <= jobb && people[center].CompareTo(person) != 0)
  54.             {
  55.                 if (people[center].CompareTo(person) == -1)
  56.                 {
  57.                     jobb = center - 1;
  58.                 }
  59.                 else
  60.                 {
  61.                     bal = center + 1;
  62.                 }
  63.                 center = (bal+jobb) / 2;
  64.             }
  65.             return bal <= jobb;
  66.         }
  67.  
  68.         private static bool ReszhalmazE(List<Person> people, List<Person> people2)
  69.         {
  70.             int j = 0;
  71.             int i = 0;
  72.             while (i < people.Count && j < people2.Count && people[i].Age >= people2[i].Age)
  73.             {
  74.                 if (people[i] == people2[i])
  75.                 {
  76.                     ++i;
  77.                 }
  78.                 ++j;
  79.             }
  80.  
  81.             return i >= j;
  82.         }
  83.     }
  84. }
  85.  
  86. ---------------------------------------------------------------------------------------------------
  87.  
  88. using System;
  89. using System.Collections.Generic;
  90. using System.Linq;
  91. using System.Text;
  92. using System.Threading.Tasks;
  93.  
  94. namespace ConsoleApp1
  95. {
  96.     internal class Person : IComparable
  97.     {
  98.         private int age;
  99.         private int salary;
  100.         private string name;
  101.  
  102.         public Person(string name, int age, int salary)
  103.         {
  104.             this.name = name;
  105.             this.age = age;
  106.             this.salary = salary;
  107.         }
  108.  
  109.         public string Name { get => this.name; set => this.name = value; }
  110.         public int Age { get => this.age; set => this.age = value; }
  111.         public int Salary { get => this.salary; set => this.salary = value; }
  112.  
  113.         public int CompareTo(object obj)
  114.         {
  115.             if (obj == null)
  116.             {
  117.                 Person p = obj as Person;
  118.  
  119.                 if (p.age > this.age) return 1;
  120.                 if (p.age < this.age) return -1;
  121.             }
  122.  
  123.             return 0;
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement