先介绍一种毒瘤的暴力(骗分过样例, 暴力出奇迹!!!~~~
只有50分
#include <cstdio>
using namespace std;
int a, b, c, d;
int read() {
int f = 1, k = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') {
f = -1;
}
c = getchar();
}
while(c <= '9' && c >= '0') {
k = k * 10 + c - '0';
c = getchar();
}
return f * k;
}
int main() {
a = read(), b = read(), c = read(), d = read();
for(double i = -100; i <= 100.00; i += 0.01) {
if(a * i * i * i + b * i * i + c * i + d < 0.0000001 && a * i * i * i + b * i * i + c * i + d > -0.0000001) {
printf("%.2f ", i);
}
}
return 0;
}
正解二分
#include <cstdio>
#include <iostream>
using namespace std;
const double j = 1e-6;
double a, b, c, d, cnt;
double f(double x) {
return a * x * x * x + b * x * x + c * x + d;
}
int main() {
cin>>a >>b >>c >>d;
for(int i = -100; i <= 100; i ++) {
if(cnt == 3) break;
double x1 = i, x2 = i + 1;
if(f(x1) == 0) {
cnt++;
printf("%.2lf ", x1);
continue;
}
if(f(x1) * f(x2) >= 0) {
continue;
}
while(x2 - x1 >= j) {
double mid = (x1 + x2) / 2;
if(f(x1) * f(mid) < 0) {
x2 = mid;
} else if(f(x2) * f(mid) < 0) {
x1 = mid;
}
}
cnt++;
printf("%.2lf ", x1);
}
return 0;
}