본문 바로가기

분류 전체보기90

백준 10818 C#) 최소, 최대 원소의 개수가 많긴 하나 시간 초과가 발생하는 문제는 아니다. int n = int.Parse(Console.ReadLine()), min, max; int[] nums = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); //1 min = max = nums[0]; for (int i = 1; i max) max = nums[i]; } Console.Write(min + " " + max); //2 min = 1000000; max = -1000000; foreach (int num in nums) { if (num < min) min .. 2022. 4. 18.
백준 1110 C#) 더하기 사이클 문제가 잘 이해되지 않는다면 예제를 살펴봐야 한다. 주어진 수가 10보다 작다면 10을 곱하라고 했는데, 그렇다면 원래 수는 10을 곱하기 전의 수가 되는 것인가? 그렇지 않다. 예제 3을 보면 1이 입력되는데 원래 수가 1일 경우에는 사이클의 길이가 60이 아닌 1이 된다. (1 + 0 = 1 이므로 한번에 원래 수 1이 나온다.) 따라서 원래 수 또한 10이 곱해진 수가 되어야 한다. 그리고 계산 도중 10보다 작은 수가 나왔다고 해서 10을 곱해줄 필요는 없다. 이는 처음 주어진 수에만 해당되는 조건이다. int n = int.Parse(Console.ReadLine()); if (n < 10) n *= 10; int temp = n, cnt = 0; do { temp = ((temp % 10) *.. 2022. 4. 18.
백준 10951 C#) A+B - 4 더 이상 입력이 존재하지 않을 때를 식으로 표현할 수 있어야 한다. StreamWriter sw = new (new BufferedStream(Console.OpenStandardOutput())); string s1; string[] s2; while ((s1 = Console.ReadLine()) != null) { s2 = s1.Split(); sw.WriteLine(int.Parse(s2[0]) + int.Parse(s2[1])); } sw.Close(); Console.ReadLine()이 null이 될 때가 바로 입력이 존재하지 않을 때이다. 2022. 4. 18.
백준 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.