๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ/C#

[์ด๊ฒƒ์ด C#์ด๋‹ค] Chapter10: ์—ฐ์Šต๋ฌธ์ œ

by HanaV 2023. 8. 21.
728x90

1. ๋‹ค์Œ ๋ฐฐ์—ด ์„ ์–ธ ๋ฌธ์žฅ ์ค‘ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์„ ๊ณ ๋ฅด์„ธ์š”.

  1.  int [ ] array = new string [3] {"์•ˆ๋…•", "Hello", "Halo"};
  2. int [ ] array = new int [3]{1,2,3};
  3. int[ ] array = new int []{1,2,3};
  4. int[ ] array = {1,2,3}; 

1๋ฒˆ์—๋Š” intํ˜• ๋ฐฐ์—ด์— string ์š”์†Œ๋ฅผ ๋„ฃ๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค.

 

2. ๋‹ค์Œ ๋‘ ํ–‰๋ ฌ A์™€ B์˜ ๊ณฑ์„ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

using System;

namespace Chapter10Practice
{
    class MainApp
    {
        static void Main(string[] args)
        {
            int[,] A = { { 3, 2 }, { 1, 4 } };
            int[,] B = { { 9, 2 }, { 1, 7 } };

            // 1. ๊ฐ๊ฐ์˜ ํ–‰, ์—ด ์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ
            int rowA = A.GetLength(0);
            int colA = A.GetLength(1);
            int rowB = B.GetLength(0);
            int colB = B.GetLength(1);

            if (colA != rowB)
            {
                Console.WriteLine("ํ–‰๋ ฌ ๊ณ„์‚ฐ ๋ถˆ๊ฐ€");
                return;
            }

            // 2. ํ–‰๋ ฌ์˜ ๊ณฑ ๊ฒฐ๊ณผ ๋ฐฐ์—ด
            int[,] answer = new int[rowA, colB];

            // 3. ํ–‰๋ ฌ ๊ณฑ ์ง„ํ–‰
            for (int i = 0; i < rowA; i++)
            {
                for (int j = 0; j < colB; j++)
                {
                    int sum = 0;
                    for (int k = 0; k < colA; k++)
                    {
                        sum += A[i, k] * B[k, j];
                    }
                    answer[i, j] = sum;
                }
            }

            // 4. ํ–‰๋ ฌ ๊ณฑ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
            for (int i = 0; i < rowA; i++)
            {
                for (int j = 0; j < colB; j++)
                {
                    Console.Write(answer[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

 

3. ๋‹ค์Œ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

Stack stack = new Stack();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);

while (stack.Count > 0)
    Console.WriteLine(stack.Pop());

stack์€ ์ˆœ์„œ๋Œ€๋กœ ์Œ“์ด๋ฏ€๋กœ ํ˜„์žฌ 1,2,3,4,5๊ฐ€ ์Œ“์—ฌ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค.
stack์˜ count๊ฐ€ 1์ด ๋  ๋•Œ๊นŒ์ง€ pop์„ ํ•˜๋ฉด ๋„ฃ์€ ์ˆœ์„œ์™€ ๋ฐ˜๋Œ€๋กœ 5,4,3,2,1์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

4. ๋‹ค์Œ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

Queue que = new Queue();
            que.Enqueue(1);
            que.Enqueue(2);
            que.Enqueue(3);
            que.Enqueue(4);
            que.Enqueue(5);

            while (que.Count > 0)
                WriteLine(que.Dequeue());

stack๊ณผ ๋ฐ˜๋Œ€๋กœ queue๋Š” ๋จผ์ € ๋“ค์–ด์˜จ ๊ฒƒ๋ถ€ํ„ฐ ๋‚˜๊ฐ„๋‹ค.
ํ˜„์žฌ 1,2,3,4,5 ์ˆœ์œผ๋กœ ๋“ค์–ด์™”์œผ๋ฏ€๋กœ, ๋‚˜๊ฐˆ ๋•Œ๋„ 1,2,3,4,5 ์ˆœ์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

 

5. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

ํšŒ์‚ฌ : Microsoft
URL : www.microsoft.com
Hashtable ht = new Hashtable();

/* 1) */ = "Microsoft";
ht["URL"] = /* 2) */;

Console.WriteLine("ํšŒ์‚ฌ : {0}", /* 3) */);
Console.WriteLine("URL : {0}", /* 4) */);

HashTable์€ key-value๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ์ฝ”๋“œ๋ฅผ ์™„์„ฑ์‹œํ‚ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

Hashtable ht = new Hashtable();
ht["ํšŒ์‚ฌ"] = "Microsoft";
ht["URL"] = "www.microsoft.com";

Console.WriteLine("ํšŒ์‚ฌ : {0}", ht["ํšŒ์‚ฌ"]);
Console.WriteLine("URL : {0}", ht["URL"]);
728x90

"); wcs_do();