一元二次算法几种源代码
//系统自带源代码#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
double a, b, c;
if (x1 < x2) {
swap(x1, x2);
}
cin >> a >> b >> c;
double delta = b * b - 4 * a * c;
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
cout << fixed << setprecision(2)<<x1 << " "
<< fixed << setprecision(2)<<x2<< endl;
return 0;
}
//本人自己写的源代码
#include<stdio.h>
#include<math.h>
int main(void)
{
float x1, x2,p;
int a, b, c;
scanf_s("%d %d %d",&a,& b,& c);
p = b*b - 4 * a * c;
if (p >= 0)
{
x1 = (-b + sqrt(p)) / (2*a);
x2 = (-b - sqrt(p)) / (2*a);
printf("%.2f %.2f", x1, x2);
}
else
printf("误解");
return 0;
}
//网上找的源代码+修改后
#include<stdio.h>
#include<math.h>
int main(void)
{
int a;
int b;
int c;
double delta;
double x1;
double x2;
scanf_s("%d %d %d",&a,& b,& c);
delta = b * b - 4 * a * c;
if (delta > 0)
{
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("x1 = %.2f, x2 = %.2f\n", x1, x2);
}
else if (delta == 0)
{
x1 = (-b + sqrt(delta)) / (2.0 * a);
x2 = x1;
printf("x1 = x2 = %f\n", x1);
}
else
{
printf("无解\n");
}
return 0;
} 你的代码打错了一个字{:10_334:} 墨羽岚 发表于 2020-4-5 17:27
你的代码打错了一个字
???那个字 1024-web1 发表于 2020-4-8 23:31
???那个字
无解不是误解 墨羽岚 发表于 2020-4-9 10:09
无解不是误解
好的,谢谢{:5_109:}
页:
[1]