반응형

연습문제 23

16. 2차원 배열 출력하기(다차원)

namespace Test { internal class Program { static void Main(string[] args) { int[,] array = new int[2, 2] { { 1, 2 }, { 3, 4 } }; int count = 0; // 1번 foreach를 이용한 출력 foreach(int i in array) { Console.Write(i + " "); count++; if (count == 2) { Console.WriteLine(); count = 0; } } Console.WriteLine(); // 2번 for문을 이용한 출력 for(int i = 0; i < array.GetLength(0); i++) { for (int j = 0; j < array.GetLeng..

14. 2차원 배열 길이 구하기

namespace Test { internal class Program { static void Main(string[] args) { int[,] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; // 2차원 배열의 길이구하기 // GetLength(0) 행을 의미 // GetLength(1) 열을 의미 Console.WriteLine(a.GetLength(0)); Console.WriteLine(a.GetLength(1)); Console.WriteLine(); // a 배열을 행열 구조로 나타내기 for(int i = 0; i < a.GetLength(0); i++) { for (int j = 0; j < a.GetLength(1); j++) Console.Write($"{a[..

13. 배열끼리 비교하여 중복제거(차집합)

namespace Test { internal class Program { static void Main(string[] args) { int[] a = { 1, 2, 3, 4, 5 }; int[] b = { 1, 3 }; // a 배열에서 b배열을 제외하기 // 방법 1. Except를 이용한 차집합 int[] c = a.Except(b).ToArray(); Console.WriteLine(string.Join(", ", c)); // 방법 2. Contains를 이용한 비교 List result = new List(); for(int i = 0; i < a.Length; i++) { if(!b.Contains(a[i])) result.Add(a[i]); } Console.WriteLine(string..

11. 문자열 접두사 비교하기

namespace Test { internal class Program { static void Main(string[] args) { // str1 문자열을 기준으로 접두사로 시작하는지 찾기 // 즉, 문자열을 앞에서부터 순서대로 비교하여 동일한 내용이 있는지 확인 // ex) a, ab, abc, abcd ... 등 // 방법 1. StartsWith를 사용하여 접두사 찾기 // 문자열 기준 str1 string str1 = "abcdef"; // 비교할 문자열 str2, str3 string str2 = "abc"; string str3 = "bcd"; bool isTrue = str1.StartsWith(str2); Console.WriteLine(isTrue); isTrue = str1.Star..

반응형