문제 풀이/백준47 백준 10871 C#) X보다 작은 수 반복문과 조건문을 혼합하면 쉽게 풀 수 있지만 다른 방식으로도 풀 수 있다. //1 StreamWriter sw = new (new BufferedStream(Console.OpenStandardOutput())); int[] nx = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); int[] a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); for (int i = 0; i < nx[0]; i++) { if (a[i] < nx[1]) { sw.Write(a[i]); sw.Write(' '); } } sw.Close(); //2 int[] nx = Array.ConvertAll(Console.R.. 2022. 4. 18. 백준 15552 C#) 빠른 A+B 이 문제에서 익혀야 하는 것은 단순히 A+B를 반복하는 방법이 아니라 최대한 입출력을 빠르게 하는 방법이다. 별로 어렵지 않기 때문에 추후 입출력이 많은 문제가 나올 때마다 써먹으면 큰 도움이 될 것이다. StreamReader sr = new (new BufferedStream(Console.OpenStandardInput())); StreamWriter sw = new (new BufferedStream(Console.OpenStandardOutput())); int n = int.Parse(sr.ReadLine()); string[] s; for (; n > 0; n--) { s = sr.ReadLine().Split(); sw.WriteLine(int.Parse(s[0]) + int.Parse(s.. 2022. 4. 18. 백준 2739 C#) 구구단 반복문의 첫 문제인 만큼 기초가 되는 내용이다. int n = Console.Read() - '0'; //1 for (int i = 1; i 2022. 4. 17. 백준 2884 C#) 알람 시계 설명 그대로 주어진 시간을 45분 당기기만 하면 된다. //1 string[] hm = Console.ReadLine().Split(); int h = int.Parse(hm[0]); int m = int.Parse(hm[1]) - 45; if (m < 0) { if (--h < 0) h = 23; m += 60; } Console.Write("{0} {1}", h, m); //2 DateTime dt = DateTime.ParseExact(Console.ReadLine(), "H m", null).AddMinutes(-45); Console.Write("{0} {1}", dt.Hour, dt.Minute); 아무래도 시간을 당기기만 하면 되는 문제라 2번 코드보다는 1번 코드가 더 가볍고 빠를 것이다... 2022. 4. 17. 백준 14681 C#) 사분면 고르기 경우의 수를 나눠서 풀 수도 있지만 조금 더 흥미로운 방법이 있다. int x = int.Parse(Console.ReadLine()); int y = int.Parse(Console.ReadLine()); //1 if (x > 0 && y > 0) Console.Write('1'); else if (x 0) Console.Write('2'); else if (x 0 && y < 0) Console.Write('4'); //2 switch (Math.Atan2(y, x)) { case < -Math.PI / 2: Console.Write('3'); break; case < 0: Console.Write.. 2022. 4. 17. 백준 9498 C#) 시험 성적 개인적으로 조건이 변수가 아닌 상수로 명확하게 나뉠 때는 switch문을 사용하는 것을 선호한다. int score = int.Parse(Console.ReadLine()); switch (score) { case >= 90: Console.Write('A'); break; case >= 80: Console.Write('B'); break; case >= 70: Console.Write('C'); break; case >= 60: Console.Write('D'); break; default: Console.Write('F'); break; } if문을 나열하는 것보다 훨씬 깔끔하다. 2022. 4. 17. 백준 2753 C#) 윤년 조건이 아주 친절하게 주어졌다 : 연도가 4의 배수이면서 (and) (100의 배수가 아닐 때 또는 (or) 400의 배수일 때) int year = int.Parse(Console.ReadLine()); //1 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) Console.Write(1); else Console.Write(0); //2 Console.Write((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 1 : 0); //3 if (year % 4 == 0 && year % 100 != 0) Console.Write(1); else if (year % 400 == 0) Console.W.. 2022. 4. 17. 백준 1330 C#) 두 수 비교하기 두 변수를 비교하여 적합한 문자를 출력하는 문제이다. string[] nums = Console.ReadLine().Split(); int a = int.Parse(nums[0]); int b = int.Parse(nums[1]); //1 if (a > b) Console.Write('>'); else if (a < b) Console.Write('' : (a < b) ? ' 2022. 4. 17. 이전 1 2 3 4 5 6 다음