11.연습문제 — 웹툰PD 지망생 : 정민재
●/섹션 2. 코드의 흐름 제어

11.연습문제

728x90

문제) 구구단 출력

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 2; i < 10; i++)
            {
                for (int j = 1; j < 10; j++)
                {
                    //Console.Write(i);
                    //Console.Write("x");
                    //Console.Write(j);
                    //Console.Write("=");
                    //Console.Write(i * j);
                    //Console.WriteLine();

                    Console.WriteLine(i + "x" + j + "=" + i * j);
                }
            }
        }
    }
}

 

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 2; i < 10; i++)
            {
                for (int j = 1; j < 10; j++)
                {
                    Console.WriteLine($"{i} * {j} ={i * j}");
                }
            }
        }
    }
}

 

문제)

 

*

**

***

****

*****

using System;

namespace Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 6; i++)      //i=1
            {
                for (int j = 1; j <= i; j++) //j=1,2,3,4,5
                {
                    Console.Write($"*");                    
                }
                Console.WriteLine();
            }
        }
    }
}

 

문제) Factorial

using System;

namespace Csharp
{
    class Program
    {
        static int Factorial(int n)
        {
            
        }

        static void Main(string[] args)
        {
            //5! = 5 * 4 * 3 * 2 * 1
            //n! = n * (n-1) ... * 1
            int ret = Factorial(5);
            Console.WriteLine(ret);
        }
    }
}

 

나의 오답

using System;

namespace Csharp
{
    class Program
    {
        static int Factorial(int n)
        {
            Console.Write($"{n}!=");

            int i;
            for (i = n; i >= 1; i--)
            {                
                Console.Write($"{i}*");               
            }

            int a = n - (n - 1);
            //Console.Write(a);

            int b = n - (n - 2);
            //Console.Write(b);

            int c = n - (n - 3);
            //Console.Write(c);

            Console.Write("=");

            return n*(n-a)*(n-b)*(n-c);
        }

        static void Main(string[] args)
        {
            //5! = 5 * 4 * 3 * 2 * 1
            //n! = n * (n-1) ... * 1
            int ret = Factorial(5);
            Console.WriteLine(ret);
        }
    }
}

 

너무 어렵게 생각해서 해괴한 코드가...

 

게다가 답도 맞지 않다.

 

 

첫 번째 답안

using System;

namespace Csharp
{
    class Program
    {
        static int Factorial(int n)
        {
            int NUM = 1;

            for (int i = 1; i <= n; i++)
            {
                NUM = NUM * i;
            }

            return NUM;
        }

        static void Main(string[] args)
        {
            //5! = 5 * 4 * 3 * 2 * 1
            //n! = n * (n-1) ... * 1
            int ret = Factorial(5);
            Console.WriteLine(ret);
        }
    }
}

 

 

두 번째 답안  - 재귀 함수 (=재귀 메소드)

using System;

namespace Csharp
{
    class Program
    {
        static int Factorial(int n)
        {
            //5! = 5 * 4 * 3 * 2 * 1
            //5! = 5 * (4!)
            return n * Factorial(n - 1);
        }

        static void Main(string[] args)
        {
            //5! = 5 * 4 * 3 * 2 * 1
            //n! = n * (n-1) ... * 1
            int ret = Factorial(5);
            Console.WriteLine(ret);
        }
    }
}

처음보는 오류...!

 

무한 뺑뺑이를 돈 결과...

 

 

두 번째 답안 수정

using System;

namespace Csharp
{
    class Program
    {
        static int Factorial(int n)
        {
            //5! = 5 * 4 * 3 * 2 * 1
            //5! = 5 * (4!)

            if (n <= 1)
            {
                return 1;
            }

            return n * Factorial(n - 1);
        }

        static void Main(string[] args)
        {
            //5! = 5 * 4 * 3 * 2 * 1
            //n! = n * (n-1) ... * 1
            int ret = Factorial(5);
            Console.WriteLine(ret);
        }
    }
}

 

728x90

' > 섹션 2. 코드의 흐름 제어' 카테고리의 다른 글

10. 오버로딩  (0) 2021.07.10
09. ref, out  (0) 2021.07.10
08. 함수(ref)  (0) 2021.07.10
07.break, continue(흐름 제어)  (0) 2021.07.10
06. for  (0) 2021.07.10