1. ๋ค์ ๋ฐฐ์ด ์ ์ธ ๋ฌธ์ฅ ์ค ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฒ์ ๊ณ ๋ฅด์ธ์.
- int [ ] array = new string [3] {"์๋ ", "Hello", "Halo"};
- int [ ] array = new int [3]{1,2,3};
- int[ ] array = new int []{1,2,3};
- 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"]);
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] ์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ: ์ปฌ๋ ์ (0) | 2023.08.22 |
---|---|
[C#] ์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ: ๋ฉ์๋, ํด๋์ค, ์ ์ฝ์กฐ๊ฑด (0) | 2023.08.22 |
[C#] ์ธ๋ฑ์ (0) | 2023.08.21 |
[C#] Collection (0) | 2023.08.21 |
[C#] ์ด์ฐจ์ ๋ฐฐ์ด๊ณผ ๊ฐ๋ณ ๋ฐฐ์ด (0) | 2023.08.21 |