19532번: 수학은 비대면강의입니다 (acmicpc.net)
- 처음에는 연립 방정식 가감법을 이용해서 이렇게 품
(arithmeticexception 예외 처리 안한 상태)
import java.util.Scanner;
//가감법 - 두 식에 적절한 수를 곱하거나 나누어 한 문자를 소거하는 방법
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a1[] = new int[3];
int a2[] = new int[3];
int x=0;
int y=0;
for(int i=0; i<3;i++)
{
a1[i] = sc.nextInt();
a2[i] = sc.nextInt();
if(a1[i]==0)
continue;
if(a2[i]==0)
continue;
}
for(int i=0; i<3; i++)
{
a1[i] = a1[i]*a2[0];
a2[i] = a2[i]*a1[0];
}
if(a1[0]!=0)
{
y= (a2[2]-a1[2])/(a2[1]-a2[1]);
x = (a1[2]-a1[1]*y)/a1[0];
}
else
{
y = a1[2]/a1[1];
x = (a2[2]-a2[1]*y)/a2[0];
}
System.out.println(x+" "+y);
}
}
- 그치만 구글링해보니 이렇게 풀 필요 없다네요
풀이과정
- -999부터 999까지인 for문 루프 2개를 이용하여 x,y 찾아냄
정답
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
int f = sc.nextInt();
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))
{
System.out.println(x+" "+y);
}
}
}
}
}
'Java > 백준' 카테고리의 다른 글
[JAVA] 백준 25305 커트라인 (0) | 2024.02.06 |
---|---|
[JAVA] 백준 2587 대표값2 (1) | 2024.02.06 |
[JAVA] 백준 2750 수 정렬하기 (1) | 2024.02.06 |
[JAVA] 백준 1436 영화감독 숌 (1) | 2024.02.06 |
[JAVA] 백준 2231 분해합 (0) | 2024.02.05 |