static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            Dictionary<string, List<Employee>> employees = new Dictionary<string, List<Employee>>();
            for (int i = 0; i < n; i++)
            {
                string[] input = Console.ReadLine().Split();
                string name = input[0];
                decimal selary = decimal.Parse(input[1]);
                string position = input[2];
                string departmen = input[3];
                string email = "n/a";
                int age = -1;

                if (input.Length >= 4)
                {
                    for (int j = 4; j < input.Length; j++)
                    {
                        try
                        {
                            age = int.Parse(input[j]);
                        }
                        catch (Exception)
                        {
                            email = input[j];
                        }
                    }

                    Employee employee = new Employee(name, selary, position, departmen, email, age);
                    if (!employees.ContainsKey(departmen))
                    {
                        employees[departmen] = new List<Employee>();
                    }

                    employees[departmen].Add(employee);
                }
            }

            var sortedEmployees = employees.OrderByDescending(x => x.Value.Sum(y => y.selary)).First();
            var orderedEmployees = sortedEmployees.Value.OrderByDescending(x => x.selary);
            Console.WriteLine("Highest Average Salary: {0}", sortedEmployees.Key);
            foreach (var employee in orderedEmployees)
            {
                Console.WriteLine("{0} {1:f2} {2} {3}", employee.name, employee.selary, employee.email, employee.age);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var departments = new Dictionary<string, List<Employee>>();

            // Input
            var n = int.Parse(Console.ReadLine());

            // Main loop
            for (var i = 0; i < n; i++)
            {
                var input = Console.ReadLine().Split(new char[] {' ','\t'},StringSplitOptions.RemoveEmptyEntries);
                Employee employeeToAdd;

                switch (input.Length)
                {
                    case 6:
                        employeeToAdd = new Employee(input[0], decimal.Parse(input[1]), input[2], input[3], input[4], int.Parse(input[5]));
                        break;
                    case 5:
                        // Check if it is email or age
                        int temp;
                        if (int.TryParse(input.Last(), out temp))
                            employeeToAdd = new Employee(input[0], decimal.Parse(input[1]), input[2], input[3],
                                age: int.Parse(input[4]));
                        else
                            employeeToAdd = new Employee(input[0], decimal.Parse(input[1]), input[2], input[3], input[4]);
                        break;
                    default:
                        employeeToAdd = new Employee(input[0], decimal.Parse(input[1]), input[2], input[3]);
                        break;
                }

                // If department exists
                if (departments.ContainsKey(input[3]))
                    departments[input[3]].Add(employeeToAdd);
                else
                    departments.Add(input[3], new List<Employee>() { employeeToAdd });
            }

            // Sort
            var sorted = departments.OrderByDescending(a => a.Value.Average(c => c.Salary));

            // Print
            Console.WriteLine("Highest Average Salary: {0}", sorted.First().Key);
            sorted.First().Value.OrderByDescending(a => a.Salary).ToList().ForEach(a => a.Print());
        }
Exemple #3
0
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());
            Employee[] employees = new Employee[count];

            for (int i=0; i < count; i++)
            {
                string input = Console.ReadLine();
                string[] tokens = input.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                string name = tokens[0];
                decimal salary = decimal.Parse(tokens[1]);
                string position = tokens[2];
                string department = tokens[3];
                if(tokens.Length == 4)
                {
                    employees[i] = new Employee(name, salary, position, department);
                }
                else if(tokens.Length == 5)
                {
                    int age;
                    bool isAge = int.TryParse(tokens[4], out age);
                    if(isAge)
                    {
                        employees[i] = new Employee(name, salary, position, department, age);
                    }
                    else
                    {
                        string email = tokens[4];
                        employees[i] = new Employee(name, salary, position, department, email);
                    }
                }
                else
                {
                    string email = tokens[4];
                    int age = int.Parse(tokens[5]);
                    employees[i] = new Employee(name, salary, position, department, email, age);
                }
            }

            Dictionary<string, decimal> totalSalaries = new Dictionary<string, decimal>();
            foreach(Employee employee in employees)
            {
                if(totalSalaries.ContainsKey(employee.department))
                {
                    totalSalaries[employee.department] += employee.salary;
                }
                else
                {
                    totalSalaries[employee.department] = employee.salary;
                }
            }

            decimal highestAverageSalary = decimal.MinValue;
            string highestPaidDepartment = "";

            foreach(string department in totalSalaries.Keys)
            {
                decimal averageSalary = totalSalaries[department] / employees.Where(e => e.department == department).Count();
                if(averageSalary > highestAverageSalary)
                {
                    highestAverageSalary = averageSalary;
                    highestPaidDepartment = department;
                }
            }

            Console.WriteLine("Highest Average Salary: {0}", highestPaidDepartment);
            foreach(Employee employee in employees.Where(e => e.department == highestPaidDepartment).OrderByDescending(e => e.salary))
            {
                Console.WriteLine("{0} {1:F2} {2} {3}", employee.name, employee.salary, employee.email, employee.age);
            }
        }