JulianJulianov

04.AsociativeArrays-LINQ-Largest 3 Numbers

Apr 12th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. II. LINQ
  2. 04. Largest 3 Numbers
  3. Read a list of integers and print the largest 3 of them. If there are less than 3, print all of them.
  4. Examples
  5. Input   Output      Input   Output
  6. 10 30 15 20 50 5    50 30 20        20 30   30 20
  7. Hints
  8. • Read an array of integers
  9. • Order the array using a LINQ query
  10.  
  11. using System;
  12. using System.Linq;
  13. using System.Collections.Generic;
  14.  
  15. namespace _03WordSynonyms
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             var listOfIntegers = Console.ReadLine().Split().Select(int.Parse).ToList();
  22.             var sortedNumbers = listOfIntegers.OrderByDescending(x => x).ToList();
  23.  
  24.             for (int i = 0; i < listOfIntegers.Count; i++)
  25.             {
  26.                 if (i < 3)
  27.                 {
  28.                     Console.Write($"{sortedNumbers[i]} ");
  29.                 }
  30.                 else
  31.                 {
  32.                     break;
  33.                 }
  34.             }
  35.         }
  36.     }
  37. }
Add Comment
Please, Sign In to add comment