#include<stdio.h>
#include<math.h>
int formula_d(int, int, int, int);
int main(void)
{
int a, b, c;
int x1, x2, tmp;
while(scanf("%d %d %d", &a, &b, &c) != EOF) {
int d = b*b - 4*a*c;
switch(formula_d(a, b, c, d)) {
case -1:
puts("No real root");
break;
case 0:
printf("Two same roots x=%d\n", -b/2*a);
break;
case 1:
x1 = (-b + sqrt(d)) / 2;
x2 = (-b - sqrt(d)) / 2;
if(x1>x2)
printf("Two different roots x1=%d, x2=%d\n", x1, x2);
else
printf("Two different roots x1=%d, x2=%d\n", x2, x1);
break;
}
}
}
int formula_d(int a, int b, int c, int d)
{
if(d>0)
return 1;
else if(d<0)
return -1;
else
return 0;
}
*** 第 5 點 (20%):WA (line:1)
您的答案為: Two same roots x=-4
正確答案為: Two same roots x=-1
但我在自己的系統上輸入 1 2 1 得到的就是-1
why??
#include<stdio.h>
#include<math.h>
int formula_d(int, int, int, int);
int main(void)
{
int a, b, c;
int x1, x2, tmp;
while(scanf("%d %d %d", &a, &b, &c) != EOF) {
int d = b*b - 4*a*c;
switch(formula_d(a, b, c, d)) {
case -1:
puts("No real root");
break;
case 0:
printf("Two same roots x=%d\n", -b/(2*a));
break;
case 1:
x1 = (-b + sqrt(d)) / (2*a);
x2 = (-b - sqrt(d)) / (2*a);
if(x1>x2)
printf("Two different roots x1=%d, x2=%d\n", x1, x2);
else
printf("Two different roots x1=%d, x2=%d\n", x2, x1);
break;
}
}
}
int formula_d(int a, int b, int c, int d)
{
if(d>0)
return 1;
else if(d<0)
return -1;
else
return 0;
}
應該是這樣才對
但是
*** 第 1 點 (20%):WA (line:1)
您的答案為: Two different roots x1=3, x2=2
正確答案為: Two different roots x1=3 , x2=2
我的程式碼中 x1=%d, x2=......
%d 和 逗點之間並無空格
是bug嗎?
#include
#include
int formula_d(int, int, int, int);
int main(void)
{
int a, b, c;
int x1, x2, tmp;
while(scanf("%d %d %d", &a, &b, &c) != EOF) {
int d = b*b - 4*a*c;
switch(formula_d(a, b, c, d)) {
case -1:
puts("No real root");
break;
case 0:
printf("Two same roots x=%d\n", -b/2*a);
break;
case 1:
x1 = (-b + sqrt(d)) / 2;
x2 = (-b - sqrt(d)) / 2;
if(x1>x2)
printf("Two different roots x1=%d, x2=%d\n", x1, x2);
else
printf("Two different roots x1=%d, x2=%d\n", x2, x1);
break;
}
}
}
int formula_d(int a, int b, int c, int d)
{
if(d>0)
return 1;
else if(d<0)
return -1;
else
return 0;
}
*** 第 5 點 (20%):WA (line:1)
您的答案為: Two same roots x=-4
正確答案為: Two same roots x=-1
但我在自己的系統上輸入 1 2 1 得到的就是-1
why??