- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
Latest commit
57 lines (48 loc) · 1.48 KB
/
Copy pathtest.py
File metadata and controls
57 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
importmath
fromdecimalimport*
getcontext().prec=100
cnt=int(input())
L=Decimal('-1000000')
H=Decimal('1000000')
tor=Decimal('1e-10')
whilecnt>0:
cnt-=1
a, b, c, d=map(Decimal, input().split())
e, f, g=Decimal('3') *a, Decimal('2') *b, c
deffn(x):
returna*x*x*x+b*x*x+c*x+d
defdet(a, b, c):
returnb*b-Decimal('4') *a*c
defsol_eq2(a,b,c):
d=det(a,b,c)
s1= (-b-d.sqrt()) /2/a
s2= (-b+d.sqrt()) /2/a
returnsorted([s1 , s2])
defsol_eq3(l, h):
whileTrue:
ifh-l<10**-15:
break
x= (l+h)/2
iffn(x)*fn(l) >0:
l=x
else:
h=x
returnx
D=det(e,f,g)
s1, s2=Decimal('0'), Decimal('0')
n=0# 실근의 개수
ifD<0: # 실근 2개 -> 기울기가 0인 지점이 2개
print(sol_eq3(L, H))
else:
s1, s2=sol_eq2(e, f, g)
ifround(fn(s1) *fn(s2), 20) >0: # 실근 1개
print(sol_eq3(L, H))
elifround(fn(s1),20) ==0andround(fn(s2),20) ==0: # 3 중근
print(s1)
elifround(fn(s1),20) ==0orround(fn(s2), 20) ==0: # 2 중근
ifround(fn(s1), 20) ==0:
print(s1, sol_eq3(s2, H))
else:
print(sol_eq3(L, s1), s2)
else: # 실근 3개
print(sol_eq3(L, s1), sol_eq3(s1,s2), sol_eq3(s2, H))