23-07-04 계륵 일기

E woo·2023년 7월 4일

계륵 일기

목록 보기
9/31
post-thumbnail

수학 공식, 브루트포스

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

  • 내 코드 (수학 공식 사용[연립 방정식 전개])
#include <iostream>

using namespace std;

int main()
{
    int a, b, c, d, e, f;
    int x, y;
    cin >> a >> b >> c >> d >> e >> f;

    int temp_b = d * b;
    int temp_e = a * e;
    int temp_c = d * c;
    int temp_f = a * f;

    if (temp_b - temp_e == 0)
        y = 0;
    else
        y = (temp_c - temp_f) / (temp_b - temp_e);

    if (a != 0)
        x = (c - b * y) / a;
    else
        x = (f - e * y) / d;

    cout << x << " " << y;

    return 0;
}
  • 다른 사람의 코드 (브루트포스 사용)
#include<iostream>

using namespace std;
int a, b, c, d, e, f;
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> a >> b >> c >> d >> e >> f;

	for (int i = -999; i <= 999; i++) {
		for (int j = -999; j <= 999; j++) {
			if ((i * a + j * b == c) && (i * d + j * e == f)) {
				cout << i << " " << j;
				return 0;
			}
			else continue;
		}
	}
}
  • 다른 사람의 코드 (연립 방정식을 공식화)
int x = (c * e - b * f) / (a * e - b * d);
int y = (c * d - a * f) / (b * d - a * e);

수학 공식 (가감범) 으로 풀 수도 있는 연립방정식이지만
시간 제한이 충분하고 정수형의 범위가 넓지 않다면
브루트포스로도 풀 수 있다.

profile
뒘벼

0개의 댓글