본문 바로가기

백준/단계별47

백준 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.
백준 9020 C#) 골드바흐의 추측 짝수를 두 소수의 합으로 나타내되 차이가 가장 작은 것으로 출력하라고 한다. 상당히 어려운 조건처럼 보이지만 이는 사실 힌트에 가깝다. public static class PS { private static int[] nArr; private static bool[] isNotPrime; public static void Main() { //최댓값에 대하여 에라토스테네스의 체를 생성하면, 이후 반복하여 만들 필요가 없다 int nMax = 0; int t; (int left, int right) ans; //입력 t = int.Parse(Console.ReadLine()); nArr = new int[t]; for (int i = 0; i < t; i++) { nArr[i] = int.Parse(Cons.. 2022. 5. 7.
백준 1929 C#) 소수 구하기 소수임을 판별해야 하는 숫자가 많을 때는, 이를 일일이 확인하기 보다는 에라토스테네스의 체를 사용하는 것이 좋다. class Program { static void Main() { //입력 string[] input = Console.ReadLine().Split(); int m = int.Parse(input[0]); int n = int.Parse(input[1]); //에라토스테네스의 체 bool[] isNotPrime = EratosSieve(n); //출력 StreamWriter sw = new(new BufferedStream(Console.OpenStandardOutput())); for (int i = m; i 2022. 5. 7.