[C++] 백준 19532 - 수학은 비대면강의 입니다.

메르센고수·2023년 8월 4일
0

Baekjoon

목록 보기
2/48

문제 - 수학은 비대면 강의 입니다. (Bronze 2)

[백준 19532] https://www.acmicpc.net/problem/19532

이 문제는 a,b,c,d,e,f 입력을 받아서 두개의 방정식의 해를 구하는 문제이다.
선형대수에서 배운 사람은 Cramer 공식을 활용하면 정말 쉽게 풀 수있고, 나 역시 Cramer 공식을 사용해서 문제를 풀었다.

참고

<Cramer's rule에 대한 설명>
https://ko.wikipedia.org/wiki/%ED%81%AC%EB%9D%BC%EB%A9%94%EB%A5%B4_%EB%B2%95%EC%B9%99

풀이 전략

  • Cramer 공식을 사용하기 위해서는 분모에 오는 행렬과 분자에 있는 행렬 2개의 행렬식이 필요하다.
  • 분모에 오는 행렬은 두 개의 방정식에서 좌항에 오는 미지수를 포함한 식의 계수행렬을 구하면 된다.
  • 분자에 오는 행렬은 분모를 구할 때 사용했던 계수행렬에서 위치에 대응하는 계수 값을 우항의 몫으로 대체하면 된다.

{ax+by=cdx+ey=f\begin{cases} ax+by=c\\ dx+ey=f \end{cases}

by Cramer's Rule

x=cbfeabdex=\frac{\begin{vmatrix}c&b\\f&e\\ \end{vmatrix}}{\begin{vmatrix}a&b\\d&e\\ \end{vmatrix}}=cebfaebd\frac{c*e-b*f}{a*e-b*d} ,

y=acdfabdey=\frac{\begin{vmatrix}a&c\\d&f\\ \end{vmatrix}}{\begin{vmatrix}a&b\\d&e\\ \end{vmatrix}}=afcdaebd\frac{a*f-c*d}{a*e-b*d}

소스 코드

#include <iostream>
using namespace std;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int a,b,c,d,e,f;
    cin>>a>>b>>c>>d>>e>>f;

    int denominator=a*e-b*d; // 분모에 오는 행렬식
    int son_x=c*e-b*f; // x 값을 구할 때 분자에 오는 행렬식
    int son_y=a*f-c*d; // y 값을 구할 때 분자에 오는 행렬식

    cout<<son_x/denominator<<" "<<son_y/denominator<<'\n';
    return 0;
}

결과

결론

Cramer 공식을 활용하면 정말 쉽게 풀 수 있다.

profile
블로그 이전했습니다 (https://phj6724.tistory.com/)

0개의 댓글