Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CrookedDigits
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- int sum = 0;
- List<string> list = new List<string>();
- foreach (var item in input)
- {
- list.Add(item.ToString());
- }
- if (list.Contains('.'.ToString()))
- {
- list.Remove('.'.ToString());
- }
- if (list.Contains('-'.ToString()))
- {
- list.Remove('-'.ToString());
- }
- List<int> result = list.Select(int.Parse).ToList();
- foreach (var item in result)
- {
- sum += item;
- }
- if (sum > 9)
- {
- while (sum > 9)
- {
- string sumAsStr = sum.ToString();
- List<int> newList = new List<int>();
- foreach (var item in sumAsStr)
- {
- int num = int.Parse(item.ToString());
- newList.Add(num);
- }
- int newSum = 0;
- foreach (var item in newList)
- {
- newSum += item;
- }
- if (newSum <= 9)
- {
- sum = newSum;
- }
- }
- }
- Console.WriteLine(sum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement