emilia98

03.SumAdjacentEqualNumbers

Jun 22nd, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03.SumAdjacentEqualNumbers_2
  6. {
  7.     class SumAdjacentEqualNumbers_2
  8.     {
  9.         static List<decimal> numbers = new List<decimal>();
  10.  
  11.         static void Main()
  12.         {
  13.             List<decimal> numbers = GetNewList();
  14.             GetProcessedList(numbers);
  15.  
  16.             string result = String.Join(" ", GetProcessedList(numbers));
  17.             Console.WriteLine(result);
  18.         }
  19.  
  20.         static List<decimal> GetNewList()
  21.         {
  22.             numbers = Console.ReadLine().Split(' ').Select(decimal.Parse).ToList();
  23.             return numbers;
  24.         }
  25.  
  26.         static List<decimal> GetProcessedList(List<decimal> numbers)
  27.         {
  28.             decimal sum = 0.0m;
  29.  
  30.             for (int cnt = 0; cnt < numbers.Count - 1; cnt++)
  31.             {
  32.                 if(cnt >= 0)
  33.                 {
  34.                     if(numbers[cnt] == numbers[cnt + 1])
  35.                     {
  36.                         sum = numbers[cnt] * 2;
  37.                         numbers[cnt] = sum;
  38.                         numbers.RemoveAt(cnt + 1);
  39.                         cnt -= 2;
  40.                     }
  41.                 }
  42.             }
  43.             return numbers;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment