Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 12.Student Academy
- Write a program that keeps information about students and their grades.
- 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.
- 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.
- Print the students and their average grade in the following format:
- {name} –> {averageGrade}
- Format the average grade to the 2nd decimal place.
- Examples
- Input Output Input Output
- 5 John -> 5.00 5 Robert -> 6.00
- John George -> 5.00 Amanda Rob -> 5.50
- 5.5 Alice -> 4.50 3.5 Christian -> 5.00
- John Amanda
- 4.5 4
- Alice Rob
- 6 5.5
- Alice Christian
- 3 5
- George Robert
- 5 6
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace 12StudentAcademy
- {
- class Program
- {
- static void Main(string[] args)
- {
- //int line = int.Parse(Console.ReadLine());
- //var students = new Dictionary<string, List<double>>();
- //for (int i = 0; i < line; i++)
- //{
- //string student = Console.ReadLine();
- //double grade = double.Parse(Console.ReadLine()); //Друго решение.
- //if (!students.ContainsKey(student))
- //students[student] = new List<double>();
- //students[student].Add(grade);
- //}
- //Console.WriteLine(string.Join($"{Environment.NewLine}",students
- //.Where(x => (x.Value.Sum() / x.Value.Count) >= 4.50)
- //.OrderByDescending(x => x.Value.Sum() / x.Value.Count)
- //.Select(x => $"{x.Key} -> {x.Value.Sum() / x.Value.Count():f2}")));
- var studentAndGrades = new Dictionary<string, List<decimal>>();
- var averageGrades = new Dictionary<string, decimal>();
- var numPair = int.Parse(Console.ReadLine());
- for (int i = 1; i <= numPair; i++)
- {
- var name = Console.ReadLine();
- var grade = decimal.Parse(Console.ReadLine());
- if (!studentAndGrades.ContainsKey(name))
- {
- studentAndGrades.Add(name, new List<decimal> { grade }); //Мое решение.
- }
- else
- {
- studentAndGrades[name].Add(grade);
- }
- }
- foreach (var item in studentAndGrades)
- {
- var averageGrade = studentAndGrades[item.Key].Average();
- averageGrades.Add(item.Key, averageGrade);
- }
- foreach (var item in averageGrades.OrderByDescending(x => x.Value))
- {
- if (item.Value >= 4.50m)
- {
- Console.WriteLine($"{item.Key} -> {item.Value:F2}");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment