【♂Not The Triumph♂O(∩_∩)O哈哈~But The Struggle♂】

竞赛决不是捷径,它只是另一种艰辛的生活方式。得到与失去,只有时间会去评判;成功与失败,只有历史能去仲裁。我不会永远成功,正如我不会永远失败一样

  C++博客 :: 首页 :: 联系 ::  :: 管理
  6 Posts :: 239 Stories :: 25 Comments :: 0 Trackbacks

常用链接

留言簿(7)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 107646
  • 排名 - 231

最新评论

阅读排行榜

评论排行榜

有形如:ax^3+bx^2+cx+d=0 这样的一个一元三次方程。给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),且根与根之差的绝对值>=1。要求由小到大依次在同一行输出这三个实根(根与根之间留有空格),并精确到小数点后2位。
提示:记方程f(x)=0,若存在2个数x1和x2,且x1<x2,f(x1)*(x2)<0,则在(x1,x2)之间一定有一个根。

input:
输入该方程中各项的系数(a,b,c,d 均为实数),

output:
由小到大依次在同一行输出这三个实根(根与根之间留有空格),并精确到小数点后2位。

input:
1 -5 -4 20

output:
-2.00 2.00 5.00

【参考程序】:

#include<cstring>
#include
<cstdio>
using namespace std;

double ans[4
];
double
 a,b,c,d;
double f(double
 xt)
{
    
double
 s;
    s
=xt*xt*xt+b*xt*xt+c*xt+
d;
    
return
 s;
}
int
 main()
{
    scanf(
"%lf%lf%lf%lf",&a,&b,&c,&
d);
    b
/=a; c/=a; d/=
a;
    
double x1,x2; int p=0
;
    
for (int x=-10000;x<=10000;x++
)
    {
        x1
=(x-0.05)/100.0; x2=(x+0.05)/100.0
;
        
if (f(x/100.0)==0 || f(x1)*f(x2)<0
)
        {
            p
++; ans[p]=x/100.0
;
        }
        
if (p==3break
;
    }
    
for (int i=1;i<=2;i++
)
        
for (int j=2;j<=3;j++
)
            
if (ans[i]>
ans[j])
            {
                
double t=ans[i];ans[i]=ans[j];ans[j]=
t;
            }
    
for (int i=1;i<=p-1;i++
)
        printf(
"%.2lf "
,ans[i]);
    printf(
"%0.2lf\n"
,ans[p]);
    
return 0
;
}

【参考程序】:
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
                inc(k);
                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.
posted on 2009-08-22 17:00 开拓者 阅读(1252) 评论(0)  编辑 收藏 引用 所属分类: 数论&几何NOIP历届题目

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理