문제 풀이49 백준 1436 C#) 영화감독 숌 또다시 각 자릿수를 추출하는 문제가 등장했다. public static class PS { public static void Main() { int n = int.Parse(Console.ReadLine()); int num = 666; int cnt = 0; while (true) { if (Check(num)) { if (++cnt == n) { Console.Write(num); return; } num += 1; } else { num += 3; } } } private static bool Check(int num) { int combo = 0; int digit; while (num > 0) { digit = num % 10; num /= 10; if (digit == 6) { if (++co.. 2022. 5. 28. 백준 1018 C#) 체스판 다시 칠하기 구현이 조금 까다로울 수도 있는 문제다. public static class PS { private static int m, n; private static char[][] board; static PS() { string[] mn = Console.ReadLine().Split(); m = int.Parse(mn[0]); n = int.Parse(mn[1]); board = new char[m][]; for (int i = 0; i < m; i++) { board[i] = Console.ReadLine().ToCharArray(); } } public static void Main() { int min = 32; for (int row = 0; row 2022. 5. 27. 백준 7568 C#) 덩치 복잡하게 생각할수록 잘 안풀리는 문제다. public static class PS { private static readonly int n; private static (int weight, int height)[] list; private static int[] rank; static PS() { n = int.Parse(Console.ReadLine()); list = new (int, int)[n]; rank = new int[n]; } public static void Main() { MakeList(); for (int i = 0; i < n; i++) { rank[i] = 1; for (int j = 0; j < n; j++) { if (i == j) continue; if (list[i].w.. 2022. 5. 25. 백준 2231 C#) 분해합 각 자릿수를 더하는 문제는 빈번하게 출제되므로 꼭 익혀두는 것이 좋다. public static class PS { public static void Main() { string ns = Console.ReadLine(); int n = int.Parse(ns); for (int m = n - ns.Length * 9; m 0) { sum += n % 10; n /= 10; } return sum; } } 이 문제를 풀다보면 문득 드는 생각이 하나 있다. 생성자의.. 2022. 5. 24. 백준 2798 C#) 블랙잭 브루트포스 단계의 첫 문제인 만큼 어렵지 않게 풀 수 있다. public static class PS { private readonly static int n, m; private static int[] cards; static PS() { string[] nm = Console.ReadLine().Split(); n = int.Parse(nm[0]); m = int.Parse(nm[1]); cards = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); } public static void Main() { int max = 0; int sum; for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n .. 2022. 5. 24. 백준 11729 C#) 하노이 탑 이동 순서 재귀로 풀 수 있는 문제들 중에서도 상당히 유명한 편이다. public static class PS { private static readonly StreamWriter sw; static PS() { sw = new(new BufferedStream(Console.OpenStandardOutput())); } public static void Main() { int n = int.Parse(Console.ReadLine()); sw.WriteLine(Math.Pow(2, n) - 1); Hanoi(n, 1, 2, 3); sw.Close(); } private static void Hanoi(int n, int from, int via, int to) { if (n == 0) return; Hanoi(n.. 2022. 5. 19. 백준 17478 C#) 재귀함수가 뭔가요? 재귀가 이루어지는 과정을 머릿속에서 그릴 수 있어야 한다. static class Program { static StreamWriter sw; static string[] dialogues; static int n; static Program() { sw = new(new BufferedStream(Console.OpenStandardOutput())); dialogues = new[] {"____", "\"재귀함수가 뭔가요?\"", "\"잘 들어보게. 옛날옛날 한 산 꼭대기에 이세상 모든 지식을 통달한 선인이 있었어.", "마을 사람들은 모두 그 선인에게 수많은 질문을 했고, 모두 지혜롭게 대답해 주었지.", "그의 답은 대부분 옳았다고 하네. 그런데 어느 날, 그 선인에게 한 선비가 찾아와서 물었어... 2022. 5. 12. 백준 10872 C#) 팩토리얼 n!은 1부터 n까지의 자연수를 모두 곱한 수를 의미한다. 참고로 0!은 1이다. int fac(int n) { if (n == 0) return 1; else return n * fac(n - 1); } int n = int.Parse(Console.ReadLine()); Console.Write(fac(n)); 팩토리얼과 피보나치는 재귀를 처음 접하는 사람들에겐 정말 안성맞춤인 문제들이다. 점화식이 어렵지 않아 다양한 변형 문제들을 풀어보면서 자연스레 반복문으로 바꿔보기도 하고 mod 연산이나 메모이제이션 등을 익힐 수도 있다. 위 문제는 입력이 한 줄만 주어지기 때문에 따로 메모이제이션은 필요 없다. 반복문으로 바꿔쓴다면 아래 코드가 될 것이다. int n = int.Parse(Console.Rea.. 2022. 5. 12. 이전 1 2 3 4 5 ··· 7 다음