Advertisement
Spocoman

01. Sum Adjacent Equal Numbers

Jan 31st, 2022 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SumAdjacentEqualNumbers
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<double> numbers = Console.ReadLine()
  12.                 .Split()
  13.                 .Select(double.Parse)
  14.                 .ToList();
  15.  
  16.             for (int i = 0; i < numbers.Count - 1; i++)
  17.             {
  18.                 if (numbers[i] == numbers[i + 1])
  19.                 {
  20.                     numbers[i] += numbers[i + 1];
  21.                     numbers.RemoveAt(i + 1);
  22.                     i= -1;
  23.                 }
  24.             }
  25.             Console.WriteLine(string.Join(" ", numbers));
  26.         }
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement