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

后端 未结 28 1230
天命终不由人
天命终不由人 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 19:55

    No loops, no conditionals, and no hardcoded literal output, aka "divide and conquer FTW" solution:

    class P
    {
        static int n;
    
        static void P1() { System.Console.WriteLine(++n); }
    
        static void P2() { P1(); P1(); }
    
        static void P4() { P2(); P2(); }
    
        static void P8() { P4(); P4(); }
    
        static void P16() { P8(); P8(); }
    
        static void P32() { P16(); P16(); }
    
        static void P64() { P32(); P32(); }
    
        static void Main() { P64(); P32(); P4(); }
    }
    

    Alternative approach:

    using System;
    
    class C
    {
        static int n;
    
        static void P() { Console.WriteLine(++n); }
    
        static void X2(Action a) { a(); a(); }
    
        static void X5(Action a) { X2(a); X2(a); a(); }
    
        static void Main() { X2(() => X5(() => X2(() => X5(P)))); }
    }
    
    0 讨论(0)
  • 2020-12-22 19:57

    One more:

    Console.WriteLine(
       String.Join(
          ", ", 
          Array.ConvertAll<int, string>(
             Enumerable.Range(1, 100).ToArray(), 
             i => i.ToString()
          )
       )
    );
    
    0 讨论(0)
  • 2020-12-22 19:57
    class Program
    {
        static int temp = 0;
    
        public static int a()
        {
            temp = temp + 1;
    
            if (temp == 100)
            {
                Console.WriteLine(temp);
                return 0;
            }
    
            else
                Console.WriteLine(temp);
    
            Program.a();
            return 0;
        }
    
        public static void Main()
        {
            Program.a();
            Console.ReadLine();
        }
    }
    
    0 讨论(0)
  • 2020-12-22 19:58
    public void Main()
    {
      printNumber(1);
    }
    
    private void printNumber(int x)
    {
      Console.WriteLine(x.ToString());
      if(x<101)
      {
        x+=1;
        printNumber(x);
      }
    }
    
    0 讨论(0)
  • 2020-12-22 19:58

    This is more or less pseudo code I havent done c# in years, PS running on 1 hour of sleep so i might be wrong.

    int i = 0;
    
    public void printNum(j){       
        if(j > 100){
            break;
        } else {
            print(j);
            printNum(j + 1);
        }
    }
    public void main(){
        print(i);
        printNum(i + 1);       
    }
    
    0 讨论(0)
  • 2020-12-22 19:59
    static void Main(string[] args)
            {
    
                print(0);
            }
    
            public static void print(int i)
            {
                if (i >= 0 && i<=10)
                {
    
                    i = i + 1;
                    Console.WriteLine(i + " ");
                    print(i);
                }
    }
    
    0 讨论(0)
提交回复
热议问题