Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Test
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             List<Employee> employees = new List<Employee>();
  14.  
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string[] input = Console.ReadLine()
  18.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 string name = input[0];
  21.                 double salaryInput = double.Parse(input[1]);
  22.                 string department = input[2];
  23.  
  24.                 Employee employee = new Employee(name,salaryInput,department);
  25.                 employees.Add(employee);
  26.             }
  27.  
  28.             string bestDepartment = string.Empty;
  29.             double bestAvarageSalary = 0.00;
  30.             double salary = 0.00;
  31.             double avarageSalary = 0.00;
  32.  
  33.             // Като ги подредя по тоя начин първо минавам през всички от един отдел,
  34.             //а като ги сортирам по заплата още от сега ми помага да ги принтирам после по лесно.
  35.             employees = employees.OrderBy(x => x.Department).ThenByDescending(x => x.Salary).ToList();
  36.  
  37.             List<string> departments = new List<string>();
  38.  
  39.             foreach (var person in employees)
  40.             {
  41.                 string department = person.Department;
  42.  
  43.                 if (departments.Count() == 0)
  44.                 {
  45.                     departments.Add(department);
  46.                     salary += person.Salary;
  47.                     bestAvarageSalary = salary;
  48.                     bestDepartment = department;
  49.                     continue;
  50.                 }
  51.                 if (!departments.Contains(department))
  52.                 {
  53.                     departments.Add(department);
  54.                     salary = person.Salary;
  55.                     if (salary > bestAvarageSalary)
  56.                     {
  57.                         bestAvarageSalary = salary;
  58.                         bestDepartment = department;
  59.                         continue;
  60.                     }
  61.                 }
  62.                 else
  63.                 {
  64.                     salary += person.Salary;
  65.                     avarageSalary = salary / 2;
  66.                     if (salary > bestAvarageSalary)
  67.                     {
  68.                         bestAvarageSalary = salary;
  69.                         bestDepartment = department;
  70.                         continue;
  71.                     }
  72.                 }
  73.             }
  74.  
  75.             Console.WriteLine($"Highest Average Salary: {bestDepartment}");
  76.  
  77.             foreach (var person in employees)
  78.             {
  79.                 if (person.Department == bestDepartment)
  80.                 {
  81.                     Console.WriteLine($"{person.Name} {person.Salary:f2}");
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     class Employee
  87.     {
  88.         public Employee(string name,double salary,string department)
  89.         {
  90.             Name = name;
  91.             Salary = salary;
  92.             Department = department;
  93.         }
  94.         public string Name { get; set; }
  95.         public double Salary { get; set; }
  96.         public string Department { get; set; }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement