2022.11.02 PM 02:30 by CBJ
來源 : https://zerojudge.tw/ShowProblem?problemid=a006 出題者 : Zerojudge 標籤 : 數學公式 難易度 : 0.1
解題想法 : 一元二次方程式公式解&判別式的應用(注意括號)(Python注意要轉int) 公式解參考 : https://zh.wikipedia.org/zh-tw/%E4%B8%80%E5%85%83%E4%BA%8C%E6%AC%A1%E6%96%B9%E7%A8%8B#%E5%85%AC%E5%BC%8F%E8%A7%A3%E6%B3%95 判別式參考 : https://zh.wikipedia.org/zh-tw/%E4%B8%80%E5%85%83%E4%BA%8C%E6%AC%A1%E6%96%B9%E7%A8%8B#%E6%A0%B9%E7%9A%84%E5%88%A4%E5%88%AB%E5%BC%8F
//C language
//solution link(含註解): https://github.com/CBJ0519/CBJsProgramDiary.com/blob/main/zj/a006.c
#include<stdio.h>
#include<math.h>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int judge=b*b-(4*a*c);
if(judge<0){
printf("No real root\n");
return 0;
}
int root1=(-b + sqrt(judge))/(2*a);
int root2=(-b - sqrt(judge))/(2*a);
if(root1==root2) printf("Two same roots x=%d\n",root1);
else if(root1>root2) printf("Two different roots x1=%d , x2=%d\n",root1,root2);
else printf("Two different roots x1=%d , x2=%d\n",root2,root1);
return 0;
}

//C++ language
//solution link(含註解): https://github.com/CBJ0519/CBJsProgramDiary.com/blob/main/zj/a006.cpp
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
int judge=b*b-(4*a*c);
if(judge<0){
printf("No real root\n");
exit(0);
}
int root1=(-b + sqrt(judge))/(2*a);
int root2=(-b - sqrt(judge))/(2*a);
if(root1==root2) cout<<"Two same roots x="<<root1<<"\n";
else cout<<"Two different roots x1="<<max(root1,root2)<<" , x2="<<min(root1,root2)<<"\n";
return 0;
}

## Python language
## solution link(含註解): https://github.com/CBJ0519/CBJsProgramDiary.com/blob/main/zj/a006.py
from math import sqrt
a,b,c=map(int,input().split())
judge=b*b-(4*a*c)
if judge<0:
print("No real root")
exit(0)
root1=int((-b + sqrt(judge))/(2*a))
root2=int((-b - sqrt(judge))/(2*a))
if root1==root2: print(f"Two same roots x={root1}")
else: print(f"Two different roots x1={max(root1,root2)} , x2={min(root1,root2)}")
##f"...{var}.."是f-string的用法, 有興趣可以去查

發佈留言