SMALL

https://www.acmicpc.net/problem/19532

 

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

정수 $a$, $b$, $c$, $d$, $e$, $f$가 공백으로 구분되어 차례대로 주어진다. ($-999 \leq a,b,c,d,e,f \leq 999$) 문제에서 언급한 방정식을 만족하는 $\left(x,y\right)$가 유일하게 존재하고, 이 때 $x$와 $y$가 각각 $-

www.acmicpc.net

 


  • 문제


  • 문제풀이

1번 코드는 그리디 알고리즘을 활용해 풀었다. 

 

2번 코드는 선형방정식을 행렬로 사용해 풀 수 있다.


  • 코드 1
using System;
using System.Collections.Generic;
using System.Globalization;

class Program
{

    static void Main()
    {
        int[] input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);

        bool find = false;
        for(int i = -999; i <= 999; i++)
        {

            for(int j = -999; j <= 999; j++)
            {
                if (input[0] * i + input[1] * j == input[2] && input[3] * i + input[4] * j == input[5])
                {
                    Console.WriteLine("{0} {1}", i, j);
                    find = true;
                    break;
                }
            }
            if (find)
            {
                break;
            }
        }
    }
}

 

  • 코드 2
using System;

class Program
{
    static void Main()
    {
        int[] input = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        int a = input[0];
        int b = input[1];
        int c = input[2];
        int d = input[3];
        int e = input[4];
        int f = input[5];

        int x = (c * e - b * f) / (a * e - b * d);
        int y = (a * f - d * c) / (a * e - b * d);

        Console.WriteLine("{0} {1}", x, y);
    }
}

  • 후기

공학수학으로 선형방정식의 행렬계산법을 배웠는데 이게 이렇게 쓰일 줄 몰랐다.

LIST

+ Recent posts