Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- class HowManyNumbers
- {
- public static List<long> FindAll(int sumDigits, int numDigits)
- {
- var list = Enumerable.Range((int)Math.Pow(10, numDigits - 1), (int)Math.Pow(10, numDigits) - 1)
- .Where(x => GetDigits(x).Sum() == sumDigits
- && GetDigits(x)
- .Zip(GetDigits(x).Skip(1), (next, curr) => next >= curr)
- .All(a => a)
- );
- if(list.Count() > 0)
- return new List<long>(){list.Count(), list.Take(1).First(), list.OrderByDescending(x => x).Take(1).First()};
- else
- return new List<long>();
- }
- public static IEnumerable<int> GetDigits(int source)
- {
- while (source > 0)
- {
- var digit = source % 10;
- source /= 10;
- yield return digit;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment