[TWIL] 멋쟁이사자처럼 부트캠프 1

용준·2024년 9월 26일

Study

목록 보기
14/22

        int a = 1;
        int b = 2;

        for (int i = 0; i < 9; i++)
        {
            a *= b;
        }

        int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        for (int i = 0; i < arr.Length / 2; i++)
        {
            int temp = arr[i];
            arr[i] = arr[arr.Length - i - 1];
            arr[arr.Length - i - 1] = temp;
        }

        bool[,] arr = new bool[5, 5];

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (i > j)
                {
                    arr[i, j] = true;
                }
                else
                {
                    arr[i, j] = false;
                }
            }
        }

        int[,] arr1 = { { 2, 2 }, { 2, 2 } };
        int[,] arr2 = { { 3, 3 }, { 3, 3 } };
        int[,] sum = new int[2, 2];

        // 행렬 합
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                sum[i, j] = arr1[i, j] + arr2[i, j];

            }
        }

        // 행렬 곱
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                sum[i, j] = arr1[i, j] * arr2[i, j];
            }
        }

        int[,] arr1 = { { 2, 2, 2 }, { 2, 2, 2 }, { 2, 2, 2 } };
        int[,] arr2 = { { 3, 3, 3 }, { 3, 3, 3 }, { 3, 3, 3 } };
        int[,] sum = new int[3, 3];

        // 행렬 합
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                sum[i, j] = arr1[i, j] + arr2[i, j];
            }
        }

        // 행렬 곱
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    sum[i, j] += arr1[i, k] * arr2[k, j];
                }
            }
        }

        int[] arr = new int[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };

        for (int i = 0; i < arr.Length - 1; i++)
        {
            for (int j = i + 1; j < arr.Length; j++)
            {
                if (arr[i] > arr[j])
                {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        int[][] arr = new int[2][];

        arr[0] = new int[5] { 3, 6, 9, 12, 15 };
        arr[1] = new int[5] { 2, 4, 6, 8, 10 };

        List<int> list = new List<int>();

        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = 0; j < arr[i].Length; j++)
            {
                if (arr[i][j] >= 10)
                {
                    list.Add(arr[i][j]);
                }
            }
        }

        foreach (var num in list)
        {
            Debug.Log(num);
        }

        List<int> result = new List<int>();

        for (int i = 1; i <= 1000; i++)
        {
            int n = i;
            bool isFiveOrZero = true;

            while (n > 0)
            {
                if (n % 5 != 0)
                {
                    isFiveOrZero = false;
                    break;
                }
                n /= 10;
            }
            if (isFiveOrZero)
            {
                result.Add(i);
            }
        }

        foreach (int n in result)
        {
            Debug.Log(n);
        }
        ```

---

![](https://velog.velcdn.com/images/kyj/post/febc3a72-f5ed-47f7-920a-196ff8e703c7/image.png)
```cs
        Dictionary<string, int> dict = new Dictionary<string, int>();

        int avg = 0;

        dict.Add("몬스터A", 100);
        dict.Add("몬스터B", 200);
        dict.Add("몬스터C", 300);
        dict.Add("몬스터D", 400);

        foreach (KeyValuePair<string, int> pair in dict)
        {
            avg += pair.Value;
        }

        Debug.Log(avg / (float)dict.Count);

        Dictionary<string, string> dic = new Dictionary<string, string>();
        Dictionary<string, string> temp = new Dictionary<string, string>();

        dic.Add("사과", "Apple");
        dic.Add("바나나", "Banana");
        dic.Add("키위", "Kiwi");
        dic.Add("오렌지", "Orange");
        dic.Add("포도", "Grape");

        foreach (KeyValuePair<string, string> pair in dic)
        {
            temp.Add(pair.Value, pair.Key);
        }

        foreach (KeyValuePair<string, string> pair in temp)
        {
            Debug.Log(pair);
        }

        List<Character> list = new List<Character>();

        list.Add(new Character("좀비A", 10));
        list.Add(new Character("좀비B", 30));
        list.Add(new Character("스켈레톤A", 20));
        list.Add(new Character("스켈레톤B", 40));

        int sum = 0, count = 0;

        foreach (Character c in list)
        {
            if (c.name.Contains("좀비"))
            {
                sum += c.atk;
                count++;
            }
        }

        Debug.Log(sum / count);
        
		// 캐릭터 구조체 //
        public struct Character
        {
            public string name;
            public int atk;

            public Character(string name, int atk)
            {
                this.name = name;
                this.atk = atk;
            }
        }

        public class Stack
        {
            List<int> data;

            public Stack()
            {
                data = new List<int>();
            }

            public void Push(int value)
            {
                data.Add(value);
            }

            public int Pop()
            {
                int rtn = data[data.Count - 1];

                data.RemoveAt(data.Count - 1);

                return rtn;
            }

            public override string ToString()
            {
                string rtn = "";

                foreach (var item in data)
                {
                    rtn += item.ToString() + " ";
                }

                return rtn;
            }
        }

        public class Queue
        {
            List<int> data;

            public Queue()
            {
                data = new List<int>();
            }

            public void Push(int value)
            {
                data.Add(value);
            }

            public int Pop()
            {
                if (data.Count > 0)
                {
                    int rtn = data[0];

                    data.RemoveAt(0);

                    return rtn;
                }

                return -9999;
            }

            public override string ToString()
            {
                string rtn = "";

                foreach(var item in data)
                {
                    rtn += item.ToString() + " ";
                }

                return rtn;
            }
        }

        bool isPalindrome(string str, bool checkCase = false, bool eraseWhiteSpace = false)
        {
            // 공백 확인
            if (eraseWhiteSpace)
            {
                // " "는 빈칸, "\t"는 탭, "\n"는 뉴 라인(줄내림)
                // Trim()은 맨 앞과 뒤쪽의 공백만 제거하므로 Replace 사용
                str = str.Replace(" ", "").Replace("\t", "").Replace("\n", "");
            }

            if (!checkCase)
            {
                // ToLower 혹은 ToUpper 아무거나 사용
                str = str.ToLower();
            }

            for (int i=0; i < str.Length / 2; i++)
            {
                if (str[i] != str[str.Length - i - 1])
                {
                    return false;
                }
            }

            return true;
        }

        public abstract class Shape 
        {
            public abstract float Area();
        }

        public class Circle : Shape
        {
            float radius;

            public Circle(float radius)
            {
                this.radius = radius;
            }

            public override float Area()
            {
                return radius * radius * Mathf.PI;
            }
        }

        public class Rectangle : Shape
        {
            float width;
            float height;

            public Rectangle(float width, float height)
            {
                this.width = width;
                this.height = height;
            }

            public override float Area()
            {
                return width * height;
            }
        }

        public class ProblemSolvingQ2 : MonoBehaviour 
        {
            void Start()
            {
                Circle circle = new Circle(10);
                Debug.Log(circle.Area());

                Rectangle rect = new Rectangle(10, 10);
                Debug.Log(rect.Area());
            }
        }

        public abstract class Employee
        {
            public string Name;
            public int EmployeeID;

            public Employee()
            {
            }

            public Employee(string name, int employeeID)
            {
                Name = name;
                EmployeeID = employeeID;
            }

            public abstract decimal CalculateMonthlySalary();
        }

        public class FullTimeEmplyoee : Employee
        {
            public decimal Salary;

            public FullTimeEmplyoee(string name, int employeeID, decimal salary)
            {
                Name = name;
                EmployeeID = employeeID;
                Salary = salary;
            }

            public override decimal CalculateMonthlySalary()
            {
                return Salary;
            }
        }

        public class PartTimeEmployee : Employee
        {
            public decimal HourSalary;
            public int HourPerMonth;

            public PartTimeEmployee(string name, int employeeID, decimal hourSalary, int hourPerMonth)
            {
                Name = name;
                EmployeeID = employeeID;
                HourSalary = hourSalary;
                HourPerMonth = hourPerMonth;
            }

            public override decimal CalculateMonthlySalary()
            {
                return HourSalary * HourPerMonth;
            }
        }

        public class ProblemSolvingQ3 : MonoBehaviour
        {
            void Start()
            {
                List<Employee> employeeList = new List<Employee>();

                FullTimeEmplyoee emplyoee1 = new FullTimeEmplyoee("철수", 1, 20000);
                employeeList.Add(emplyoee1);

                PartTimeEmployee employee2 = new PartTimeEmployee("잡스", 2, 20000, 120);
                employeeList.Add(employee2);

                decimal totalSalary = 0;

                foreach(Employee employee in employeeList)
                {
                    totalSalary += employee.CalculateMonthlySalary();
                }
            }
        }

        public class Book
        {
            public string Title;
            public string Author;
            private bool isBorrowed;
            public bool IsBorrowed { get { return IsBorrowed; } }

            public Book(string title, string author)
            {
                Title = title;
                Author = author;
                isBorrowed = false;
            }

            public void Borrow()
            {
                isBorrowed = true;
            }

            public void Return()
            {
                isBorrowed = false;
            }

            public override string ToString()
            {
                return Title + " by " + Author;
            }
        }

        public class Library
        {
            List<Book> books;

            public Library()
            {
                books = new List<Book>();
            }

            public void AddBook(Book book)
            {
                books.Add(book);
            }

            public List<Book> SearchBooksByAuthor(string author, bool onlyAvailableBooks = false)
            {
                List<Book> result = new List<Book>();

                foreach (Book book in books)
                {
                    if (book.Author == author && (!onlyAvailableBooks || book.IsBorrowed == false))
                    {
                        result.Add(book);
                    }
                }

                return result;
            }

            public void Borrow(Book book)
            {
                if (books.Contains(book))
                {
                    book.Borrow();
                }
            }

            public void Return(Book book)
            {
                if (books.Contains(book))
                {
                    book.Return();
                }
            }
        }

        public class ProblemSolvingQ4 : MonoBehaviour
        {
            void Start()
            {
                Library library = new Library();

                Book book1 = new Book("책1", "권씨");
                library.AddBook(book1);

                Book book2 = new Book("책2", "김씨");
                library.AddBook(book2);

                Book book3 = new Book("책3", "박씨");
                library.AddBook(book3);

                Book book4 = new Book("책4", "이씨");
                library.AddBook(book4);

                List<Book> result = library.SearchBooksByAuthor("이씨");

                library.Borrow(book3);

                library.SearchBooksByAuthor("이씨", true);

                foreach (Book book in result)
                {
                    Debug.Log(book);
                }
            }
        }

0개의 댓글