본문 바로가기
Development/Baekjoon

[C#] 19532번: 수학은 비대면강의입니다

by Mobics 2025. 10. 8.

목차


    백준 단계별로 풀어보기

    25.10.08

    12단계: 브루트 포스


    19532번: 수학은 비대면강의입니다

    문제 링크 : https://www.acmicpc.net/problem/19532

     

    문제 풀이

    >> 문제를 풀기 위해 알아야 할 개념

    - 연립방정식의 해 구하기

    1. 대입법

    : 두 식 중, 어느 한 식에서 한 미지수를 다른 미지수로 표현한 뒤, 다른 식에 그대로 대입하여 계산하는 방법

     

    2. 가감법

    : 두 연립 방정식에서 한 미지수의 계수를 같게 만든 뒤, 계수를 맞춘 두 식을 더하거나 빼서 한 미지수를 소거하여 계산하는 방법

     

    >> 풀이

    : 브루트 포스 방식의 풀이

    1. a, b, c, d, e, f 를 입력받아 int값으로 변환한다.
    2. 이중 for문을 통해 x와 y의 범위인 -999부터 999까지 반복하며 연립방정식을 모두 만족하는 x, y값을 찾는다.
    3. 적합한 x, y값을 찾으면 x, y값을 출력하고 코드를 종료한다.

     

    정답 코드

    class Backjoon
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            int a = int.Parse(input[0]);
            int b = int.Parse(input[1]);
            int c = int.Parse(input[2]);
            int d = int.Parse(input[3]);
            int e = int.Parse(input[4]);
            int f = int.Parse(input[5]);
    
            for (int x = -999; x <= 999; x++)
            {
                for (int y = -999; y <= 999; y++)
                {
                    if (a * x + b * y == c && d * x + e * y == f)
                    {
                        Console.WriteLine($"{x} {y}");
                        return;
                    }
                }
            }
        }
    }1

     

    ※ 대입법으로 풀어보기

    class Backjoon
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            int a = int.Parse(input[0]);
            int b = int.Parse(input[1]);
            int c = int.Parse(input[2]);
            int d = int.Parse(input[3]);
            int e = int.Parse(input[4]);
            int f = int.Parse(input[5]);
            
            // ax + by = c	-> 1번 식
            // dx + ey = f	-> 2번 식
            // ey = f - dx
            	// y = (f - dx) / e
            // ax + b(f - dx) / e = c	-> 1번 식에 y = (f - dx) / e 대입
            // ax - bdx + bf / e = c
            // (a - bd)x = ce - bf
            	// x = (ce - bf) / (a - bd)
    
            int x = (c * e - b * f) / (a - b * d);
            int y = (f - d * x) / e;
    
            Console.Write($"{x} {y}");
        }
    }

    'Development > Baekjoon' 카테고리의 다른 글

    [C#] 1436번: 영화감독 숌  (0) 2025.10.12
    [C#] 1018번: 체스판 다시 칠하기  (0) 2025.10.11
    [C#] 2231번: 분해합  (0) 2025.10.07
    [C#] 2798번: 블랙잭  (0) 2025.10.06
    [C#] 24313번: 알고리즘 수업 - 점근적 표기 1  (1) 2025.10.04