Advertisement
marekov

Crooked_Digits

Apr 5th, 2020
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CrookedDigits
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = Console.ReadLine();
  12.             int sum = 0;
  13.             List<string> list = new List<string>();
  14.  
  15.             foreach (var item in input)
  16.             {
  17.                 list.Add(item.ToString());
  18.             }
  19.             if (list.Contains('.'.ToString()))
  20.             {
  21.                 list.Remove('.'.ToString());
  22.             }
  23.             if (list.Contains('-'.ToString()))
  24.             {
  25.                 list.Remove('-'.ToString());
  26.             }
  27.  
  28.             List<int> result = list.Select(int.Parse).ToList();
  29.             foreach (var item in result)
  30.             {
  31.                 sum += item;
  32.             }
  33.  
  34.             if (sum > 9)
  35.             {
  36.                 while (sum > 9)
  37.                 {
  38.                     string sumAsStr = sum.ToString();
  39.                     List<int> newList = new List<int>();
  40.  
  41.                     foreach (var item in sumAsStr)
  42.                     {
  43.                         int num = int.Parse(item.ToString());
  44.                         newList.Add(num);
  45.                     }
  46.                     int newSum = 0;
  47.                     foreach (var item in newList)
  48.                     {
  49.                         newSum += item;
  50.                     }
  51.  
  52.                     if (newSum <= 9)
  53.                     {
  54.                         sum = newSum;
  55.                     }
  56.                 }
  57.             }
  58.             Console.WriteLine(sum);
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement