#include<stdio.h>
#include<math.h>
int main(void)
{
int rootnum;
int a,b,c;//方程式的系數
int x1,x2;//根
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
rootnum=(b*b-4*a*c);
if(rootnum>0){
if((-b+sqrt(b*b-4*a*c))/2*a>(-b-sqrt(b*b-4*a*c))/2*a){
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
else{
x1=(-b-sqrt(b*b-4*a*c))/2*a;
x2=(-b+sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
}
else if(rootnum<0)
printf("No real root\n");
else
printf("Two same roots x=%d\n",-b/2*a);
}
return 0;
}
程式碼如上,我又遇到自行測試與最終結果不同的情況了...
#include
#include
int main(void)
{
int rootnum;
int a,b,c;//方程式的系數
int x1,x2;//根
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
rootnum=(b*b-4*a*c);
if(rootnum>0){
if((-b+sqrt(b*b-4*a*c))/2*a>(-b-sqrt(b*b-4*a*c))/2*a){
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
else{
x1=(-b-sqrt(b*b-4*a*c))/2*a;
x2=(-b+sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
}
else if(rootnum<0)
printf("No real root\n");
else
printf("Two same roots x=%d\n",-b/2*a);
}
return 0;
}
程式碼如上,我又遇到自行測試與最終結果不同的情況了...
抱歉我打錯題目了 正確的題目是a006
#include
#include
int main(void)
{
int rootnum;
int a,b,c;//方程式的系數
int x1,x2;//根
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
rootnum=(b*b-4*a*c);
if(rootnum>0){
if((-b+sqrt(b*b-4*a*c))/2*a>(-b-sqrt(b*b-4*a*c))/2*a){
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
else{
x1=(-b-sqrt(b*b-4*a*c))/2*a;
x2=(-b+sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
}
else if(rootnum<0)
printf("No real root\n");
else
printf("Two same roots x=%d\n",-b/2*a);
}
return 0;
}
程式碼如上,我又遇到自行測試與最終結果不同的情況了...
抱歉我打錯題目了 正確的題目是a006
if((-b+sqrt(b*b-4*a*c))/2*a>(-b-sqrt(b*b-4*a*c))/2*a){
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
2*a?應該要是 (2 * a) 吧?這樣會是前面的結果先除以2,再乘以a喔。