C# - ๋ฐฐ์ด๊ณผ ์ปฌ๋ ์ ์ ๋ฆฌ - 1์ฐจ์๋ถํฐ ๋ค์ฐจ์, ๋ฆฌ์คํธ๋ถํฐ ๋์ ๋๋ฆฌ๊น์ง + ์์
๐ ๋ฐฐ์ด๊ณผ ์ปฌ๋ ์
๋ฐฐ์ด
1์ฐจ์ ๋ฐฐ์ด
๋์ผํ ๋ฐ์ดํฐ ์ ํ์ ์ฐ์์ ์ผ๋ก ์ ์ฅํ๋ฉฐ, ์ธ๋ฑ์ค๋ฅผ ํตํด ์์์ ์ ๊ทผํ ์ ์๋ค. ๋ฐฐ์ด์ ์ ์ธ ์ ๊ณ ์ ๋ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๋ฉฐ, ํฌ๊ธฐ ์ด์์ ๋ฐ์ดํฐ๋ ์ ์ฅํ ์ ์๋ค.
int[] array1 = new int[5];
array1[0] = 1;
...
... ์์ : ๋ฐฐ์ด์ ํ์ฉํ ํฉ๊ณ ๊ณ์ฐ
int[] itemPrices = { 100, 200, 300, 400, 500 };
int totalPrice = 0;
for (int i = 0; i < itemPrices.Length; i++)
{
totalPrice += itemPrices[i];
}
Console.WriteLine("์ด ์์ดํ
๊ฐ๊ฒฉ: " + totalPrice + " gold");
๋ฐฐ์ด ์ค์ต ์์
- ๊ฒ์ ์บ๋ฆญํฐ ๋ฅ๋ ฅ์น ๋ฐฐ์ด
- ํ์ ์ฑ์ ํ๊ท ๊ณ์ฐ
- ๋ฐฐ์ด ๊ธฐ๋ฐ ์ซ์ ๋ง์ถ๊ธฐ ๊ฒ์
๋ค์ฐจ์ ๋ฐฐ์ด
ํ๊ณผ ์ด๋ก ๊ตฌ์ฑ๋ 2์ฐจ์ ์ด์์ ๋ฐฐ์ด ๊ตฌ์กฐ๋ก, ํ ํํ์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ๋ ์ฌ์ฉ๋๋ค.
int[,] array2D = new int[2, 3];
int[,,] array3D = new int[2, 3, 4];
์์ : 2์ฐจ์ ๋ฐฐ์ด์ ํ์ฉํ ๊ฒ์ ๋งต
int[,] map = {
{1,1,1,1,1},
{1,0,0,0,1},
...
};
์ปฌ๋ ์
์ปฌ๋ ์ ์ ๋ฐฐ์ด๊ณผ ์ ์ฌํ์ง๋ง, ํฌ๊ธฐ๊ฐ ๊ฐ๋ณ์ ์ด๋ฉฐ ๋ค์ํ ๊ธฐ๋ฅ์ ์ง์ํ๋ ์๋ฃ ๊ตฌ์กฐ๋ค. C#์ ์ปฌ๋ ์ ์ System.Collections.Generic ๋ค์์คํ์ด์ค์ ์ ์๋์ด ์๋ค.
List
๋์ ์ผ๋ก ํฌ๊ธฐ๊ฐ ์กฐ์ ๋๋ ๋ฐฐ์ด ๊ตฌ์กฐ๋ก, ์์์ ์ถ๊ฐ/์ญ์ /์ฝ์ ์ด ๊ฐ๋ฅํ๋ค. ๋ด๋ถ์ ์ผ๋ก ๋ฐฐ์ด์ ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ฉฐ, ์์๊ฐ ๋ง์์ง ๊ฒฝ์ฐ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํด ๋ฐ์ดํฐ๋ฅผ ๋ณต์ฌํ๋ ๋ฐฉ์์ผ๋ก ํ์ฅ๋๋ค.
List<int> numbers = new List<int>();
numbers.Add(1); numbers.Remove(1);
Dictionary
ํค์ ๊ฐ์ ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๊ตฌ์กฐ๋ค. ํค๋ ์ค๋ณต๋ ์ ์์ผ๋ฉฐ, ๊ฐ์ ๋น ๋ฅด๊ฒ ์ ๊ทผํ ์ ์๋ ์ฅ์ ์ด ์๋ค.
Dictionary<string, int> scores = new Dictionary<string, int>();
scores.Add("Alice", 100);
Stack
ํ์ ์ ์ถ(LIFO) ๊ตฌ์กฐ๋ก ๋์ํ๋ฉฐ, Push()๋ก ์ถ๊ฐํ๊ณ Pop()์ผ๋ก ์ ๊ฑฐํ๋ค.
Stack<int> stack = new Stack<int>();
stack.Push(1); int val = stack.Pop();
Queue
์ ์ ์ ์ถ(FIFO) ๊ตฌ์กฐ๋ก ๋์ํ๋ฉฐ, Enqueue()๋ก ์ถ๊ฐํ๊ณ Dequeue()๋ก ์ ๊ฑฐํ๋ค.
Queue<int> queue = new Queue<int>();
queue.Enqueue(1); int val = queue.Dequeue();
HashSet
์ค๋ณต์ ํ์ฉํ์ง ์๋ ์งํฉ ๊ตฌ์กฐ๋ค. ์ฝ์ ์์๊ฐ ๋ณด์ฅ๋์ง ์์ผ๋ฉฐ, Contains()๋ก ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ ์ ์๋ค.
HashSet<int> set = new HashSet<int>();
set.Add(1); set.Contains(1);
๋ฐฐ์ด vs ๋ฆฌ์คํธ
ํญ๋ชฉ | ๋ฐฐ์ด | ๋ฆฌ์คํธ |
ํฌ๊ธฐ | ๊ณ ์ | ๊ฐ๋ณ |
๋ฉ๋ชจ๋ฆฌ | ์ ์ ํ ๋น | ๋์ ๋ฐฐ์ด ๊ตฌ์กฐ |
๊ธฐ๋ฅ | ๋จ์ ์ ์ฅ/์ ๊ทผ | ์ฝ์ , ์ญ์ , ํ์ ๋ฑ ๋ค์ |
์ฑ๋ฅ | ๋ฐ๋ณต์ ์ ๋ฆฌ | ์ ์ฐ์ฑ ๋ฐ์ด๋จ |
- ๋ฐ์ดํฐ ํฌ๊ธฐ๊ฐ ๊ณ ์ ๋๊ฑฐ๋ ๋ฐ๋ณต ์ฐ์ฐ ์ค์ฌ์ผ ๊ฒฝ์ฐ ๋ฐฐ์ด ์ฌ์ฉ์ด ์ ๋ฆฌํ๋ค.
- ์ ๋์ ์ธ ๋ฐ์ดํฐ ์ฒ๋ฆฌ, ์ฝ์ /์ญ์ ๊ฐ ์ฆ์ ๊ฒฝ์ฐ ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ํจ์จ์ ์ด๋ค.
๊ฒ์์ผ๋ฆญํฐ ๋ฅ๋ ฅ์น ๋ง๋ค๊ธฐ
// ํ๋ ์ด์ด์ ๊ณต๊ฒฉ๋ ฅ, ๋ฐฉ์ด๋ ฅ, ์ฒด๋ ฅ, ์คํผ๋๋ฅผ ์ ์ฅํ ๋ฐฐ์ด
int[] playerStats = new int[4];
// ๋ฅ๋ ฅ์น๋ฅผ ๋๋ค์ผ๋ก ์์ฑํ์ฌ ๋ฐฐ์ด์ ์ ์ฅ
Random rand = new Random();
for (int i = 0; i < playerStats.Length; i++)
{
playerStats[i] = rand.Next(1, 11);
}
// ๋ฅ๋ ฅ์น ์ถ๋ ฅ
Console.WriteLine("ํ๋ ์ด์ด์ ๊ณต๊ฒฉ๋ ฅ: " + playerStats[0]);
Console.WriteLine("ํ๋ ์ด์ด์ ๋ฐฉ์ด๋ ฅ: " + playerStats[1]);
Console.WriteLine("ํ๋ ์ด์ด์ ์ฒด๋ ฅ: " + playerStats[2]);
Console.WriteLine("ํ๋ ์ด์ด์ ์คํผ๋: " + playerStats[3]);
ํ์๋ค์ ์ฑ์ ํ๊ท ๊ตฌํ๊ธฐ
int[] scores = new int[5]; // 5๋ช
์ ํ์ ์ฑ์ ์ ์ ์ฅํ ๋ฐฐ์ด
// ์ฑ์ ์
๋ ฅ ๋ฐ๊ธฐ
for (int i = 0; i < scores.Length; i++)
{
Console.Write("ํ์ " + (i + 1) + "์ ์ฑ์ ์ ์
๋ ฅํ์ธ์: ");
scores[i] = int.Parse(Console.ReadLine());
}
// ์ฑ์ ์ดํฉ ๊ณ์ฐ
int sum = 0;
for (int i = 0; i < scores.Length; i++)
{
sum += scores[i];
}
// ์ฑ์ ํ๊ท ์ถ๋ ฅ
double average = (double)sum / scores.Length;
Console.WriteLine("์ฑ์ ํ๊ท ์ " + average + "์
๋๋ค.");
๋ฐฐ์ด์ ํ์ฉํ ์ซ์ ๋ง์ถ๊ธฐ ๊ฒ์
Random random = new Random(); // ๋๋ค ๊ฐ์ฒด ์์ฑ
int[] numbers = new int[3]; // 3๊ฐ์ ์ซ์๋ฅผ ์ ์ฅํ ๋ฐฐ์ด
// 3๊ฐ์ ๋๋ค ์ซ์ ์์ฑํ์ฌ ๋ฐฐ์ด์ ์ ์ฅ
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = random.Next(1, 10);
}
int attempt = 0; // ์๋ ํ์ ์ด๊ธฐํ
while (true)
{
Console.Write("3๊ฐ์ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์ (1~9): ");
int[] guesses = new int[3]; // ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์ซ์๋ฅผ ์ ์ฅํ ๋ฐฐ์ด
// ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์ซ์ ๋ฐฐ์ด์ ์ ์ฅ
for (int i = 0; i < guesses.Length; i++)
{
guesses[i] = int.Parse(Console.ReadLine());
}
int correct = 0; // ๋ง์ถ ์ซ์์ ๊ฐ์ ์ด๊ธฐํ
// ์ซ์ ๋น๊ต ๋ฐ ๋ง์ถ ๊ฐ์ ๊ณ์ฐ
for (int i = 0; i < numbers.Length; i++)
{
for (int j = 0; j < guesses.Length; j++)
{
if (numbers[i] == guesses[j])
{
correct++;
break;
}
}
}
attempt++; // ์๋ ํ์ ์ฆ๊ฐ
Console.WriteLine("์๋ #" + attempt + ": " + correct + "๊ฐ์ ์ซ์๋ฅผ ๋ง์ถ์
จ์ต๋๋ค.");
// ๋ชจ๋ ์ซ์๋ฅผ ๋ง์ถ ๊ฒฝ์ฐ ๊ฒ์ ์ข
๋ฃ
if (correct == 3)
{
Console.WriteLine("์ถํํฉ๋๋ค! ๋ชจ๋ ์ซ์๋ฅผ ๋ง์ถ์
จ์ต๋๋ค.");
break;
}
}
๊ฒ์ ๋งต์ ๊ตฌํ
int[,] map = new int[5, 5]
{
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 }
};
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (map[i, j] == 1)
{
Console.Write("โ ");
}
else
{
Console.Write("โก ");
}
}
Console.WriteLine();
}