How to print 1 to 100 without any looping using C#

后端 未结 28 1231
天命终不由人
天命终不由人 2020-12-22 19:30

I am trying to print numbers from 1 to 100 without using loops, using C#. Any clues?

相关标签:
28条回答
  • 2020-12-22 20:10

    With regular expressions

    using System.Text.RegularExpressions;
    
    public class Hello1
    {
       public static void Main()
       {
    
          // Count to 128 in unary
          string numbers = "x\n";
          numbers += Regex.Replace(numbers, "x+\n", "x$&");
          numbers += Regex.Replace(numbers, "x+\n", "xx$&");
          numbers += Regex.Replace(numbers, "x+\n", "xxxx$&");
          numbers += Regex.Replace(numbers, "x+\n", "xxxxxxxx$&");
          numbers += Regex.Replace(numbers, "x+\n", "xxxxxxxxxxxxxxxx$&");
          numbers += Regex.Replace(numbers, "x+\n", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$&");
          numbers += Regex.Replace(numbers, "x+\n", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$&");
    
          // Out of 1..128, select 1..100
          numbers = Regex.Match(numbers, "(.*\n){100}").Value;
    
          // Convert from unary to decimal
          numbers = Regex.Replace(numbers, "x{10}", "<10>");
          numbers = Regex.Replace(numbers, "x{9}", "<9>");
          numbers = Regex.Replace(numbers, "x{8}", "<8>");
          numbers = Regex.Replace(numbers, "x{7}", "<7>");
          numbers = Regex.Replace(numbers, "x{6}", "<6>");
          numbers = Regex.Replace(numbers, "x{5}", "<5>");
          numbers = Regex.Replace(numbers, "x{4}", "<4>");
          numbers = Regex.Replace(numbers, "x{3}", "<3>");
          numbers = Regex.Replace(numbers, "x{2}", "<2>");
          numbers = Regex.Replace(numbers, "x{1}", "<1>");
          numbers = Regex.Replace(numbers, "(<10>){10}", "<100>");
          numbers = Regex.Replace(numbers, "(<10>){9}", "<90>");
          numbers = Regex.Replace(numbers, "(<10>){8}", "<80>");
          numbers = Regex.Replace(numbers, "(<10>){7}", "<70>");
          numbers = Regex.Replace(numbers, "(<10>){6}", "<60>");
          numbers = Regex.Replace(numbers, "(<10>){5}", "<50>");
          numbers = Regex.Replace(numbers, "(<10>){4}", "<40>");
          numbers = Regex.Replace(numbers, "(<10>){3}", "<30>");
          numbers = Regex.Replace(numbers, "(<10>){2}", "<20>");
          numbers = Regex.Replace(numbers, "(<[0-9]{3}>)$", "$1<00>");
          numbers = Regex.Replace(numbers, "(<[0-9]{2}>)$", "$1<0>");
          numbers = Regex.Replace(numbers, "<([0-9]0)>\n", "$1\n");
          numbers = Regex.Replace(numbers, "<([0-9])0*>", "$1");
    
          System.Console.WriteLine(numbers);
    
       }
    }
    

    The output:

    # => 1
    # => 2
    # ...
    # => 99
    # => 100
    
    0 讨论(0)
  • 2020-12-22 20:10

    Just LINQ it...

    Console.WriteLine(Enumerable.Range(1, 100)
                                .Select(s => s.ToString())
                                .Aggregate((x, y) => x + "," + y));
    
    0 讨论(0)
  • 2020-12-22 20:11
    Enumerable.Range(1, 100).ToList().ForEach(i => Console.WriteLine(i));
    

    Here's a breakdown of what is happening in the above code:

    • Enumerable.Range returns the specified range of integral numbers as IEnumerator<Int32>
    • Enumerable.ToList<T> converts an IEnumerable<T> into a List<T>
    • List<T>.ForEach takes a lamdba function and invokes it for each item in the list

    Performance Consideration

    The ToList call will cause memory to be allocated for all items (in the above example 100 ints). This means O(N) space complexity. If this is a concern in your app i.e. if the range of integers can be very high, then you should avoid ToList and enumerate the items directly.

    Unfortunately ForEach is not part of the IEnumerable extensions provided out of the box (hence the need to convert to List in the above example). Fortunately this is fairly easy to create:

    static class EnumerableExtensions
    {
        public static void ForEach<T>(this IEnumerable<T> items, Action<T> func)
        {
            foreach (T item in items)
            {
                func(item);
            }
        }
    }
    

    With the above IEnumerable extension in place, now in all the places where you need to apply an action to an IEnumerable you can simply call ForEach with a lambda. So now the original example looks like this:

    Enumerable.Range(1, 100).ForEach(i => Console.WriteLine(i));
    

    The only difference is that we no longer call ToList, and this results in constant (O(1)) space usage... which would be a quite noticeable gain if you were processing a really large number of items.

    0 讨论(0)
  • 2020-12-22 20:12
    using static IronRuby.Ruby;
    
    class Print1To100WithoutLoopsDemo
    {
        static void Main() => 
          CreateEngine().Execute("(1..100).each {|i| System::Console.write_line i }");
    }
    

    Hey, why not?

    0 讨论(0)
  • 2020-12-22 20:12

    My solution is in thread 2045637, which asks the same question for Java.

    0 讨论(0)
  • 2020-12-22 20:13

    I can think two ways:

    • using 100 Console.WriteLine
    • using goto in a switch statement
    0 讨论(0)
提交回复
热议问题