JulianJulianov

12.AssociativeArrays-Student Academy

Mar 16th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. 12.Student Academy
  2. Write a program that keeps information about students and their grades.
  3. You will receive n pair of rows. First you will receive the student's name, after that you will receive his grade. Check if the student already exists and if not, add him. Keep track of all grades for each student.
  4. When you finish reading the data, keep the students with average grade higher than or equal to 4.50. Order the filtered students by average grade in descending order.
  5. Print the students and their average grade in the following format:
  6. {name} –> {averageGrade}
  7. Format the average grade to the 2nd decimal place.
  8.  
  9. Examples
  10. Input                   Output                    Input                   Output
  11. 5                       John -> 5.00              5                       Robert -> 6.00
  12. John                    George -> 5.00            Amanda                  Rob -> 5.50
  13. 5.5                     Alice -> 4.50             3.5                     Christian -> 5.00
  14. John                                              Amanda
  15. 4.5                                               4
  16. Alice                                             Rob
  17. 6                                                 5.5
  18. Alice                                             Christian
  19. 3                                                 5
  20. George                                            Robert
  21. 5                                                 6
  22.  
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Linq;
  26.  
  27. namespace 12StudentAcademy
  28. {
  29.    class Program
  30.    {
  31.        static void Main(string[] args)
  32.        {
  33.            //int line = int.Parse(Console.ReadLine());
  34.            //var students = new Dictionary<string, List<double>>();
  35.  
  36.            //for (int i = 0; i < line; i++)
  37.            //{
  38.                //string student = Console.ReadLine();
  39.                //double grade = double.Parse(Console.ReadLine());                           //Друго решение.
  40.  
  41.                //if (!students.ContainsKey(student))
  42.                    //students[student] = new List<double>();
  43.  
  44.                //students[student].Add(grade);
  45.            //}
  46.  
  47.            //Console.WriteLine(string.Join($"{Environment.NewLine}",students
  48.                //.Where(x => (x.Value.Sum() / x.Value.Count) >= 4.50)
  49.                //.OrderByDescending(x => x.Value.Sum() / x.Value.Count)
  50.                //.Select(x => $"{x.Key} -> {x.Value.Sum() / x.Value.Count():f2}")));
  51.  
  52.            var studentAndGrades = new Dictionary<string, List<decimal>>();
  53.            var averageGrades = new Dictionary<string, decimal>();
  54.            var numPair = int.Parse(Console.ReadLine());
  55.            for (int i = 1; i <= numPair; i++)
  56.            {
  57.                var name = Console.ReadLine();
  58.                var grade = decimal.Parse(Console.ReadLine());
  59.                if (!studentAndGrades.ContainsKey(name))
  60.                {
  61.                    studentAndGrades.Add(name, new List<decimal> { grade });                     //Мое решение.
  62.                }
  63.                else
  64.                {
  65.                    studentAndGrades[name].Add(grade);
  66.                }
  67.            }
  68.            foreach (var item in studentAndGrades)
  69.            {
  70.                var averageGrade = studentAndGrades[item.Key].Average();
  71.                averageGrades.Add(item.Key, averageGrade);
  72.            }
  73.            foreach (var item in averageGrades.OrderByDescending(x => x.Value))
  74.            {
  75.                if (item.Value >= 4.50m)
  76.                {
  77.                    Console.WriteLine($"{item.Key} -> {item.Value:F2}");
  78.                }
  79.            }
  80.        }
  81.    }
  82. }
Add Comment
Please, Sign In to add comment