Advertisement
Guest User

Logs Aggregator

a guest
May 12th, 2018
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 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 _10
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int times = int.Parse(Console.ReadLine());
  14.             List<User> AllUsers = new List<User>();
  15.             bool alreadyExistsUser = false;
  16.             for (int i = 0; i < times; i++)
  17.             {
  18.                 string[] input = Console.ReadLine().Split(' ');
  19.                 string inputIp = input[0];
  20.                 string inputName = input[1];
  21.                 int inputDuration = int.Parse(input[2]);
  22.                 foreach (var user in AllUsers)
  23.                 {
  24.                     if (user.Name == inputName)
  25.                     {
  26.                         alreadyExistsUser = true;
  27.                         user.duration += inputDuration;
  28.                         if (!user.Ips.Contains(inputIp))
  29.                         {
  30.                             user.Ips.Add(inputIp);
  31.                             break;
  32.                         }
  33.                         else
  34.                         {
  35.                             break;
  36.                         }
  37.                     }                
  38.                 }
  39.                 if (!alreadyExistsUser)
  40.                 {
  41.                     AllUsers.Add(new User(inputName, inputIp, inputDuration));
  42.                 }
  43.             }
  44.             AllUsers = AllUsers.OrderBy(a => a.Name).ToList();
  45.             foreach (var user in AllUsers)
  46.             {
  47.                 user.Ips.Sort();
  48.                 Console.Write($"{user.Name}: {user.duration} [");
  49.                 string outIp = "";
  50.                 foreach (var ip in user.Ips)
  51.                 {
  52.                     outIp += $"{ip}, ";
  53.                 }
  54.                 outIp = outIp.Remove(outIp.Length - 2);
  55.                 outIp += "]";
  56.                 Console.WriteLine(outIp);
  57.             }
  58.         }
  59.         class User
  60.         {
  61.             public string Name;
  62.             public int duration = 0;
  63.             public List<string> Ips = new List<string>();
  64.             public User (string Name , string FirstIp , int FirstDuration)
  65.             {
  66.                 this.Name = Name;
  67.                 Ips.Add(FirstIp);
  68.                 duration += FirstDuration;
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement