197 条题解
-
0yuxinhao LV 8 @ 2016-06-15 10:54:21
``c++
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;int a,b,c,d;
double f(double x);
int jie();
int main()
{
cin>>a>>b>>c>>d;jie();
return 0;
}
double f(double x)
{
return(x*x*x*a+x*x*b+x*c+d);
}
int jie()
{
int x;
double xx;
double x1,x2;
for(x=-100;x<=100;x++)
{
x1=x;x2=x+1;
if(f(x1)==0)printf("%.2f ", x1);
else if(f(x1)*f(x2)<0)
{
while(x2-x1>=0.001)
{
xx=(x1+x2)/2;
if(f(x1)*f(xx)<=0)x2=xx;
else x1=xx;
}printf("%.2f ", x1);
}}
cout<<endl;
}
为什么是错的,大神求解,拜托,十分感谢! -
02016-05-08 22:03:55@
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;int main()
{
double a,b,c,d;
double as,bs,t,si;
double x1,x2,x3;cin>>a>>b>>c>>d;
as=b*b-3*a*c;
bs=b*c-9*a*d;t= (2*as*b-3*a*bs)/(2*sqrt(as*as*as));
si=acos(t);
x1=(-b-2*sqrt(as)*cos(si/3))/(3*a);
x2=(-b+sqrt(as)*(cos(si/3)+sqrt(3)*sin(si/3)))/(3*a);
x3=(-b+sqrt(as)*(cos(si/3)-sqrt(3)*sin(si/3)))/(3*a);cout << fixed << setprecision(2)<<x1<<" ";
cout << fixed << setprecision(2)<<x3<<" ";
cout << fixed << setprecision(2)<<x2<<" ";return 0;
}盛金公式
-
02016-01-25 16:23:51@
Pascal AC
其实abs(ax^3+bx^2+cx+d)<0.01 即可,估计很多人都是=0所以过不了
var a,b,c,d,x:real;
i:longint;
begin
read(a,b,c,d);
for i:=-10000 to 10000 do
if abs(a*(i/100)*(i/100)*(i/100)+b*(i/100)*(i/100)+c*(i/100)+d)<0.01 then
begin
x:=i/100;
write(x:0:2,' ');
end;
end. -
02015-12-16 17:40:19@
AC 40 留念
-
02015-11-21 13:11:37@
枚举即可
#include <cstdio>
#include <cstdlib>using namespace std;
double a, b, c, d;
double val(double x) {
return (a * x * x * x + b * x * x + c * x + d);
}
int main(int argc, const char *argv[]) {
scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
for (double i = -100.00; i <= 100.00; i += 0.01) {
double p = val(i);
if (-0.00001 < p && p < 0.00001)
printf("%.2f ", i);
}
return 0;
} -
02015-10-25 00:10:13@
#include <iostream>
#include <cstdio>using namespace std;
int main()
{
double a,b,c,d;
cin>>a>>b>>c>>d;
double i,j;
j=-100.00;
i=j;
for(j;j<=100;j+=0.01)
{
if(i*i*i*a+i*i*b+i*c+d<0)
if(j*j*j*a+j*j*b+j*c+d>0)
{
printf("%.2f ",j);
i=j;
}
if(i*i*i*a+i*i*b+i*c+d>0)
if(j*j*j*a+j*j*b+j*c+d<0)
{
printf("%.2f ",j);
i=j;
}
}
return 0;
} -
02015-09-04 17:23:59@
`//要用Fabs()
#include <cstdlib>
#include <cmath>
#include <string>
#include <iostream>using namespace std;
double a,b,c,d;
double ans[10];double f(double x)
{
double y;
y = a * x * x * x + b * x * x + c * x + d;
return y;
}int main()
{
int num = 0;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
for(double i = -100.0; i<= 100.0;i+=0.01)
{
if(fabs(i)<=0.000001) {num++; ans[num]=i; }
}
for(int j=1;j<num;j++) printf("%.2lf ",ans[j]);
printf("%.2lf",ans[num]);
return 0;
}` -
02015-07-03 18:58:33@
还是二分法高大上,总感觉枚举没水平。
代码#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
#define swap(a,b) {double t=a;a=b;b=t;}
#define p 0.001
double A,B,C,D;
double f(double x) //定义一元三次函数
{
return (A*x*x*x+B*x*x+C*x+D);
}
int s(double &x,double l,double r) //二分法
{
double m;
m=(l+r)/2;
if((f(m)==0)||((r-l)<p))x=m;
else
if((f(m)*f(l))<0)s(x,l,m);
else s(x,m,r);
return 0;
}
int main()
{
double x1,x2,x3,a,b,d;
cin>>A>>B>>C>>D;
//A,B,C,D为方程系数
//x1,x2,x3为方程的三个解
//a,b为有解区间,m为a,b中点
//p表示精度,d为判别式
//定义三次函数
//对此三次函数进行求导可得 f'(x)=3*A*x*x+2*B*x+C
//此二次函数的判别式为
//△= b*b-4ac
// = (2*B)*(2*B)-4*(3*A)*C
// = 4*B*B-12*A*C
d=4*B*B-12*A*C;
//此二次函数的零点即方程f'(x)=0的解为
//x=(-b±√△)/(2*a)即三次函数的极值
a=((-2*B+sqrt(d))/(6*A));
b=((-2*B-sqrt(d))/(6*A));
if(a>b)swap(a,b);//设a<b
//再对(-∞,a)、(a,b)、(b,+∞)进行二分
s(x1,-100.0,a);
s(x2,a,b);
s(x3,b,100.0);
cout<<fixed<<setprecision(2)\
<<x1<<' '<<x2<<' '<<x3;
return 0;
} -
02015-05-04 13:40:01@
AC100留念。
只要枚举就可以了,不用二分。Pascal Code
const
min=-100.0;
max=100.0;
eps=0.001;
var
num:longint;
a,b,c,d,i:double;
function f(x:double):double;
begin
exit(a*x*x*x+b*x*x+c*x+d);
end;
function isroot(x:double):boolean;
begin
exit(abs(f(x))<eps);
end;
begin
readln(a,b,c,d);
num:=0;
i:=min;
repeat
if isroot(i) then begin write(i:0:2,' ');num:=num+1;i:=i+1;end
else i:=i+eps;
until (num>=3) or (i>max);
end. -
02015-04-10 12:44:36@
program san;
var a,b,c,d,x,fx:real;
i,j:integer;
f:array[-100..100] of real;
begin
readln(a,b,c,d);
for i:=-100 to 100 do f[i]:=a*i*i*i+b*i*i+c*i+d;
for i:=-100 to 99 do
begin
if abs(f[i])<0.0000001 then begin x:=i; write(x:0:2,' '); end
else if f[i]*f[i+1]<0 then
begin
x:=i;
for j:=1 to 99 do
begin
x:=x+0.01;
fx:=a*x*x*x+b*x*x+c*x+d;
if abs(fx)<0.0000001 then write(x:0:2,' ');
end;
end;
end;
if 1000000*a+10000*b+100*c+d<0.0000001 then write('100.00',' ');
end. -
02015-01-03 13:59:20@
二分法查找(比枚举其实效率高N倍)(当然此题精度太低,如果换到4位小数你就知道了);
###block code
program ex;
var a,b,c,d,x,left,right:double;
i,n:longint;
function f(xx:double):double;
begin
f:=a*xx*xx*xx+b*xx*xx+c*xx+d;
end;procedure main(ll,rr:double);
var i:longint;
mid:double;
begin
mid:=(ll+rr)/2;if (ll+0.01>rr) or (f(mid)=0) then
begin
write(mid:0:2,' ');
exit;
end;if f(ll)*f(mid)<0 then
main(ll,mid)
else
main(mid,rr);end;
begin //main
read(a,b,c,d);
left:=-101; right:=-100; n:=0;for i:=1 to 200 do
begin
if n=3 then
break;left:=left+1; right:=right+1;
if f(left)=0 then
begin
write(left:0:2,' ');
inc(n);
continue;
end;if f(left)*f(right)<0 then
begin
main(left,right);
inc(n);
end
else
continue;
end;end.
-
02014-12-23 21:11:14@
枚举法
###block code
program ex;
var a,b,c,d,x:double;
n,i:longint;
begin
read(a,b,c,d); n:=0;
for i:=-10000 to 10000 do
begin
x:=0.01*i;
if abs(a*x*x*x+b*x*x+c*x+d)<0.000000001 then
begin
inc(n);
write(x:0:2,' ');
end;
if n=3 then
exit;
end;end.
-
02014-10-28 22:37:21@
没人吗。。。。。。。。。
-
02014-10-27 22:39:38@
有没有好心人帮我看一下这段代码哪里出问题了,为什么是50分。。。
#include <iostream>
#include <cmath>
#include<iomanip>
using namespace std;
float a,b,c,d,i,x,p,q,y;
float f(float x) {
return x*(x*(x*a+b)+c)+d;
}
int main(int argc, char** argv) {
cin>>a>>b>>c>>d;
for (i=-100;i<=100;i++){
p=i;q=i+0.99;
if(f(p)==0) cout<<fixed<<setprecision(2)<<p<<' ';
else if(f(q)==0) cout<<fixed<<setprecision(2)<<q<<' ';
else {
while (f(p)*f(q)<0){
y=(p+q)/2;
if (f(y)==0||p+0.001>q) {
cout<<fixed<<setprecision(2)<<y;
break;}
else if (f(p)*f(y)<0) q=y;
else if (f(q)*f(y)<0) p=y;
}
}
}
return 0;
} -
02014-10-24 23:04:33@
我才发现我写的好麻烦……
var a,b,c,d,x1,x2,tt:double;
x,i,j,k:longint;
ans:array[1..3]of double;
function f(xx:double):double;
begin
f:=xx*xx*xx+b*xx*xx+c*xx+d;
end;
begin
while not eof do
begin
read(a,b,c,d);
b:=b/a;c:=c/a;d:=d/a;a:=1;
k:=0;
for x:=-10000 to 10000 do
begin
x1:=(x-0.005)/100;x2:=(x+0.005)/100;
if (f(x1)*f(x2)<0)or(f(x2)=0) then
begin
k:=k+1;
ans[k]:=x/100;
end;
end;
for i:=1 to 2 do
for j:=i+1 to 3 do
if ans[i]>ans[j] then
begin
tt:=ans[i];ans[i]:=ans[j];ans[j]:=tt;
end;
for i:=1 to 2 do write(ans[i]:0:2,' ');
writeln(ans[3]:0:2);
end;
end. -
02014-08-31 14:41:55@
数据太小,枚举即可
代码奉上:
#include <iostream>
#include <cstdio>using namespace std;
const double e=1e-2;
const double eps=1e-6;double a,b,c,d;
double f(double x)
{
return a*x*x*x+b*x*x+c*x+d;
}int main()
{
double ans=-100.00;
cin>>a>>b>>c>>d;
for(;ans<=100.00;ans+=e)
{
if(f(ans)<eps && f(ans)>-eps)
{
printf("%.2lf ",ans);
}
}return 0;
} -
02014-08-19 15:04:11@
f'x 为二次方程,有两解为极值
可以从 (-200,l) (l,r) (r, 200) 二分
也可 newton(-200), newton((l+r)/2), newton(200)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
struct math_function{
double a[4];
math_function() {
memset(a, 0, sizeof(a));
}
double count(double x) {
double res = 0;
for(int i = 3; i > -1 ; --i) {
res *= x;
res += a[i];
}
return res;
}
}f, df;double newton(double w) {
double x = w;
do{
w = x;
x = w - f.count(w)/df.count(w);
}while(abs(x-w)>0.001);
return x;
}
void getfx() {
scanf("%lf%lf%lf%lf", &f.a[3], &f.a[2], &f.a[1], &f.a[0]);
}
void getdfx() {
for(int i=1; i < 4; ++i)
df.a[i-1] = i*f.a[i];
}
void resolve() {
double mid;
mid = -df.a[1]/(2*df.a[2]);
printf("%.2lf %.2lf %.2lf\n", newton(-200), newton(mid), newton(200));}
int main() {
getfx();
getdfx();
resolve();
return 0;
} -
02014-08-10 14:49:19@
Block code
var trying:array[-100..100] of real;
a,b,c,d:real;
x:array[1..3] of real;
i,rootnumber:longint;function y(x:real):real;
begin
y:=a*x*x*x+b*x*x+c*x+d;
end;function qiugen(low,high:real):real;
var t,k:real;
begin
if high-low<1e-3 then
qiugen:=(high+low)/2
else
begin
t:=(high+low)/2;
k:=y(t)*y(high);
if k<0 then
qiugen:=qiugen(t,high)
else if k>0 then
qiugen:=qiugen(low,t)
else
qiugen:=t;
end;
end;begin
readln(a,b,c,d);
for i:=-100 to 100 do
trying[i]:=y(i);
rootnumber:=1;
for i:=-100 to 99 do
begin
if rootnumber>3 then break;
if trying[i]=0 then
begin
x[rootnumber]:=i;
inc(rootnumber);
end
else
if trying[i]*trying[i+1]<0 then
begin
x[rootnumber]:=qiugen(i,i+1);
inc(rootnumber);
end;
end;
writeln(x[1]:0:2,' ',x[2]:0:2,' ',x[3]:0:2);
end. -
02014-07-31 22:38:31@
枚举……1次居然就过了
#include <iostream>
#include <cmath>
#include <iomanip>using namespace std;
int main()
{
float idx = 0;
float a = 0, b = 0, c = 0, d = 0;cin >> a >> b >> c >> d;
for (idx = -10000; idx <= 10000; idx ++)
{
if (abs(a * pow(idx / 100, 3) + b * pow(idx / 100, 2) + c * idx / 100 + d) < 0.001)
{
cout << setiosflags(ios::fixed);
cout << setprecision(2) << idx / 100 << " ";
}
}
return 0;
}