Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //Write a program to sum all adjacent equal numbers in a list of decimal numbers, starting from left to right.
- //After two numbers are summed, the obtained result could be equal to some of its neighbors and should be summed as well(see the examples below).
- //Always sum the leftmost two equal neighbors(if several couples of equal neighbors are available).
- namespace Sum_Adjacent_Equal_Numbers
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<decimal> list = Console.ReadLine().Split(' ').Select(decimal.Parse).ToList();
- for (int index = 0; index < list.Count - 1; index++)
- {
- if (list[index] == list[index+1])
- {
- list[index] += list[index + 1];
- list.RemoveAt(index + 1);
- index-=2;
- if (index < -1)
- {
- index = -1;
- }
- }
- }
- Console.WriteLine(string.Join(" ", list));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement