코딩도장

허근까지 나타내는 이차방정식

  • 전에 올라온 이차방정식 문제가 2개 있었지만, 허근 표기는 하지 않았습니다.
  • 이 문제에서는 허근까지 나타내어야 합니다.
  • 실근, 중근의 경우에는 수치로 나타내지만, 허근의 경우에는 a+bi(단, i=루트 -1) 꼴의 문자열로 나타내어야 합니다.
  • 허근일 경우, a가 0이면 bi 꼴로만 출력해야 하며, b가 1이면 a+i 꼴로만 출력해야 합니다.
  • 입력값은 이차항의 계수, 일차항의 계수, 상수항이며, 해를 구하기 위해 어떤 방식을 사용할지는 자유입니다.
  • 해는 실수부분과 허수부분이 모두 정수가 되도록 반올림해야 합니다.

예시 input

1 4 5

예시 output

-2 + i, -2 - i
수학 sqrt format

2021年07月03日 09:27

이준우

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

2개의 풀이가 있습니다.

C#

using System;
using System.Numerics;
namespace 허근까지
{
 class Program
 {
 static void Main()
 {
 var solution = new QuadraticEquation(1, 4, 5);
 Console.WriteLine(solution);
 }
 }
 class QuadraticEquation
 {
 private readonly double quadraticCoeff, linearCoeff, constantCoeff;
 public QuadraticEquation(double quadraticCoeff, double linearCoeff, double constantCoeff)
 {
 this.quadraticCoeff = quadraticCoeff; // 2차항 계수
 this.linearCoeff = linearCoeff; // 1차항 계수
 this.constantCoeff = constantCoeff; // 상수항 계수
 this.Discriminant = Math.Pow(linearCoeff, 2.0) - 4.0 * this.quadraticCoeff * this.constantCoeff;
 }
 // 판별식
 public double Discriminant { get; }
 // 해를 문자열 표현으로 반환
 public override string ToString()
 {
 Complex solution1 = (-linearCoeff + Complex.Sqrt(Discriminant)) / (2.0 * quadraticCoeff);
 Complex solution2 = (-linearCoeff - Complex.Sqrt(Discriminant)) / (2.0 * quadraticCoeff);
 string expression1 = Complex2Expression(solution1);
 string expression2 = Complex2Expression(solution2);
 return Discriminant == 0 ? $"{expression1}" : $"{expression1}, {expression2}";
 }
 // 복소수 클래스를 문자열 표현으로 변환
 private static string Complex2Expression(Complex complex)
 {
 // 해의 정수 반올림
 int realValue = (int)Math.Round(complex.Real);
 int imagValue = (int)Math.Round(complex.Imaginary);
 // 해를 경우따른 문자열로 표현
 string first = realValue != 0 ? $"{realValue}" : "";
 string sign = imagValue < 0 ? "-" : "+";
 string second = Math.Abs(imagValue) != 1 ? $"{Math.Abs(imagValue)}i" : "i";
 if (imagValue == 0)
 {
 second = string.Empty;
 sign = string.Empty;
 }
 string expression = $"{first}{sign}{second}";
 if (expression == "")
 {
 expression = "0";
 }
 return expression;
 }
 }
}

2021年08月20日 11:20

mohenjo

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
using System;
namespace solution
{
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine("이차방정식 ax2 + bx + c = 0 에서 a b c 을 입력하세요.(예: 1 -3 2)");
 string[] coeff = Console.ReadLine().Split(' ');
 Console.WriteLine("{0}x2 + {1}x + {2} = 0 의 해: {3}",coeff[0], coeff[1], coeff[2], solution(coeff));
 }
 private static string solution(string[] coeff)
 {
 string ans = "";
 double a = double.Parse(coeff[0]);
 double b = double.Parse(coeff[1]);
 double c = double.Parse(coeff[2]);
 double D = b * b - 4 * a * c;
 string strD = "";
 if (D < 0)
 {
 int rootD = (int)Math.Round(Math.Sqrt(-D) / (2 * a));
 strD = rootD == 1 ? "i" : rootD.ToString() + "i";
 if (-b / (2 * a) == 0)
 ans = strD + " , -" + strD;
 else
 ans = Math.Round(-b/(2*a)) + " + " + strD + " , " + Math.Round(-b / (2 * a)) + " - " + strD;
 }
 else if (D > 0)
 {
 double dd = Math.Sqrt(D) / (2 * a);
 Console.WriteLine(-b / (2 * a) + " : "+ dd);
 ans = Math.Round((-b - Math.Sqrt(D)) / (2 * a)).ToString() + " , " + Math.Round((-b + Math.Sqrt(D)) / (2 * a)).ToString();
 }
 else
 ans = Math.Round(-b/(2*a)).ToString();
 return ans;
 }
 }
}

2023年06月18日 19:48

insperChoi

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

풀이 작성

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

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

수학 x 6
sqrt x 1
format x 1
연관 문제

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

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