반응형

전체 글 158

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..

10. 공백(다중) 처리하기

namespace Test { internal class Program { static void Main(string[] args) { // 기본적인 공백기준 확인방법 // Split은 해당부분을 기준으로 나눈다 string str = "hello world!"; string[] strings = str.Split(' '); for(int i = 0; i < strings.Length; i++) { Console.WriteLine($"{i} = {strings[i]}"); } Console.WriteLine(); Console.WriteLine(); // 그런데 위에 방식대로 단순 Split를 쓸경우 // 아래와 같이 다중으로 공백이 들어가면 문제발생 str = " hello world! "; strin..

9. 제곱근과 제곱수

namespace Test { internal class Program { static void Main(string[] args) { // 제곱근 구하기 // Math.Sqrt => (double)반환형 int n = 9; double result = Math.Sqrt(n); Console.WriteLine(result); Console.WriteLine("======"); // 제곱수인지 판별하는법 (제곱근 % 1) // Math.Sqrt로 나온 결과물에 1로 나눈 나머지가 // 0이면 n은 제곱수이다. n = 10; result = Math.Sqrt(n) % 1; Console.WriteLine(result); n = 9; result = Math.Sqrt(n) % 1; Console.WriteLi..

반응형