코딩도장

연립일차방정식의 해 계산기

연립일차방정식은 ax+by+c=0, a'x+b'y+c'=0 의 형태이다, 이때 연립일차방정식의 해를 자동으로 구해주는 프로그램을 만들어라.

  • 연립일차방정식의 풀이법은 가감법과 대입법이 있다. 가감법은 연립일차방정식의 두 일차방정식에 적당한 수를 곱한 후 두 방정식을 빼 한 미지수의 해를 구하고 다른 미지수의 해를 구할 수 있고, 대입법은 예를 들어 y = x + 2같이 연립일차방정식의 한 일차방정식을 적당하게 이항해 만든 후 다른 연립일차방정식의 일차방정식에 대입시켜 한 미지수의 값을 구한 후 다른 미지수 값을 구한다.
  • (x+2)/5 + (2y+2)/7 = 2/5, x/2 + y/3 = 5같이 a,a',b,b'에 분수가 들어가는 것은 제외시켜도 된다.
  • 해가 특수한 경우는 추가해야 한다.
  • 가감법을 추천드립니다.
  • 문제에 대한 문제가 있는 경우 알려주세요. 수정하겠습니다.

2022年02月09日 22:35

고태욱

(追記) (追記ここまで)
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

1개의 풀이가 있습니다.

자바로 풀어봤습니다.

import java.util.Scanner;
public class test {
 // x 계수 찾기
 public static Integer findCoeffx(String equation) {
 if(equation.split("x|X")[0].equals("")) {
 return 1;
 }else if(equation.split("x|X")[0].equals("-")){
 return -1;
 }else if((equation.contains("x")|equation.contains("X"))==false){
 return 0;
 }else {
 return Integer.parseInt(equation.split("x|X")[0]);
 }
 }
 // y 계수 찾기
 public static Integer findCoeffy(String equation) {
 if(equation.split("x|X|y|Y")[1].equals("+")) {
 return 1;
 }else if(equation.split("x|X|y|Y")[1].equals("-")){
 return -1;
 }else if(equation.split("y|Y")[0].equals("")|equation.split("y|Y")[0].equals("-")) {
 return 0;
 }else if((equation.contains("y")|equation.contains("Y"))==false) {
 return 0;
 }else {
 return Integer.parseInt(equation.split("x|X|y|Y")[1]);
 }
 }
 // 상수 찾기
 public static Integer findConstant(String equation) {
 if(equation.contains("=")==false) {
 return 0;
 }else {
 return Integer.parseInt(equation.split("=")[1]);
 }
 }
 public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);
 String[] equations = new String[2];
 int[] coeffx = new int[2];
 int[] coeffy = new int[2];
 int[] constant = new int[2];
 // 일차방정식 입력 받기
 int count = 0; 
 float answery, answerx;
 String inputX, inputY; 
 while(true) {
 System.out.printf("%d번째 일차방정식(ax+by=c):", count+1);
 equations[count] = scan.next();
 // 일차방정식의 계수 뽑아서 배열에 저장
 coeffx[count] = findCoeffx(equations[count]);
 coeffy[count] = findCoeffy(equations[count]);
 constant[count] = findConstant(equations[count]);
 // 만약 계수가 0이면 다시 입력
 if(coeffx[count]==0|coeffy[count]==0|constant[count]==0) {
 System.out.println("잘못 입력하셨습니다. 다시 입력하시오.");
 }else {
 count++;
 }
 // 방정식을 2개 입력했는지 
 if(count==2) {
 break;
 }
 }
 // y 값 구하기
 int diffy = coeffy[0]*coeffx[1] - coeffy[1]*coeffx[0];
 int diffConstant = constant[0]*coeffx[1] - constant[1]*coeffx[0]; 
 answery = (float)diffConstant/diffy;
 // x 값 구하기
 answerx = (constant[0]-coeffy[0]*answery)/coeffx[0];
 // 결과 출력
 System.out.printf("x : %f\n", answerx);
 System.out.printf("y : %f", answery);
 }
}

2022年06月12日 19:57

유로

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

풀이 작성

(注記) 풀이작성 안내
  • 본문에 코드를 삽입할 경우 에디터 우측 상단의 "코드삽입" 버튼을 이용 해 주세요.
  • 마크다운 문법으로 본문을 작성 해 주세요.
  • 풀이를 읽는 사람들을 위하여 풀이에 대한 설명도 부탁드려요. (아이디어나 사용한 알고리즘 또는 참고한 자료등)
  • 작성한 풀이는 다른 사람(빨간띠 이상)에 의해서 내용이 개선될 수 있습니다.
풀이 작성은 로그인이 필요합니다.
목록으로
코딩도장

코딩도장은 프로그래밍 문제풀이를 통해서 코딩 실력을 수련(Practice)하는 곳입니다.


언어별 풀이 현황
전 체 x 12
python x 9
기 타 x 2
java x 1
코딩도장 © 2014 · 문의 [email protected]
피드백 · 개인정보취급방침 · RSS

AltStyle によって変換されたページ (->オリジナル) /