fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7. class RangeInfo
  8. {
  9. public int Start {get; private set;}
  10. public int Last {get; private set;}
  11. public int Length { get { return Last - Start + 1; } }
  12. public RangeInfo(int start, int last)
  13. {
  14. this.Start = start;
  15. this.Last = last;
  16. }
  17. }
  18.  
  19. static IEnumerable<RangeInfo> Solve726(int n)
  20. {
  21. int last = (int)(-1 + Math.Sqrt(1.0 + 8 * n) / 2);
  22. int first = 0;
  23. int d = last * (last + 1) / 2 - n;
  24. while (first <= last)
  25. {
  26. switch (Math.Sign(d))
  27. {
  28. case -1: d += ++last; break;
  29. case 1: d -= first++; break;
  30. default:
  31. yield return new RangeInfo(first, last);
  32. d -= first++;
  33. break;
  34. }
  35. }
  36. }
  37.  
  38. static void Main(string[] args)
  39. {
  40. int N = 2014;
  41. foreach (var x in Solve726(N))
  42. {
  43. var r = Enumerable.Range(x.Start, x.Length).Select(n => n.ToString());
  44. Console.WriteLine(string.Join(",", r.ToArray()));
  45. }
  46. }
  47. }
  48.  
Success #stdin #stdout 0.03s 33864KB
stdin
Standard input is empty
stdout
12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115
502,503,504,505
2014