Math Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

Problem Description
Here has an function:
  f(x)=|a∗x3+b∗x2+c∗x+d|(L≤x≤R)
Please figure out the maximum result of f(x).
 
Input
Multiple test cases(less than 100). For each test case, there will be only 1 line contains 6 numbers a, b, c, d, L and R. (−10≤a,b,c,d≤10,−100≤L≤R≤100)
 
Output
For each test case, print the answer that was rounded to 2 digits after decimal point in 1 line.
 
Sample Input
1.00 2.00 3.00 4.00 5.00 6.00
 
Sample Output
310.00
 
 
代码:
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm> using namespace std; // f(x)=|a∗x3+b∗x2+c∗x+d|(L≤x≤R) int main()
{
double a, b, c, d, ll, r;
double mm, dd, ff;
double gg, hh; while(cin>>a)
{
cin>>b>>c>>d>>ll>>r; dd=(a*pow(ll, 3.0)+b*pow(ll, 2.0)+c*ll+d);
if(dd<0)
dd=-dd;
ff=(a*pow(r, 3.0)+b*pow(r, 2.0)+c*r+d);
if(ff<0)
ff=-ff;
mm=max(dd, ff); if((4*b*b - 12*a*c)<0)
{
//无解
printf("%.2lf\n", mm);
continue;
}
else if( (4*b*b - 12*a*c)==0 )
{
//有一个解
if( (-(b)/(3*a))>=ll && (-(b)/(3*a))<=r )
{
gg=-1*(b/3*a);
hh=a*pow(gg,3)+b*pow(gg, 2)+c*gg+d;
if(hh<0)
hh=-hh;
if(hh>mm)
mm=hh;
printf("%.2lf\n", mm);
continue;
}
}
else
{
//有2个解
gg=(-b+sqrt(b*b-4*a*c))/2*a;
if( gg>=ll && gg<=r )
{
double q;
q=a*pow(gg,3)+b*pow(gg, 2)+c*gg+d;
if(q<0)
q=-q;
if(q>mm)
mm=q;
} hh=(-b*sqrt(b*b-4*a*c))/2*a;
if(hh>=ll && hh<=r)
{
double w;
w=a*pow(hh,3)+b*pow(hh, 2)+c*hh+d;
if(w<0)
w=-w;
if(w>mm)
mm=w;
} printf("%.2lf\n", mm);
}
} return 0;
}

最新文章

  1. Android 几种消息推送方案总结
  2. prototype和__proto__
  3. python redis使用心得
  4. 关于mysql和Apache以及nginx的监控脚本怎么写会比较好的记录
  5. 【LR】录制测试脚本中的基本菜单
  6. scala知识点(二)
  7. Delphi XE5 如何设计并使用FireMonkeyStyle(转)
  8. css案例学习之div ul li a 实现导航效果
  9. 在MySQL中创建实现自增的序列(Sequence)的教程
  10. (简单) POJ 3259 Wormholes,SPFA判断负环。
  11. 自然梯度(Natural Gradient)
  12. 命令行参数处理-getopt()和getopt_long()
  13. flask_login 整合 pyjwt + json 简易flask框架
  14. Linuxc - gdb调试程序
  15. 10位时间戳使用moment转化为日期
  16. LeetCode算法题-Construct the Rectangle(Java实现)
  17. bat命令查询硬件信息
  18. JavaScript中date 对象常用方法
  19. Nestjs 使用mongodb
  20. [leetcode]152. Maximum Product Subarray最大乘积子数组

热门文章

  1. 使用jersey组件向图片资源服务器上传图片报403,405,409 Method Not Allowed错误
  2. 浅析PropertySource 基本使用
  3. Java中将List转成逗号数组的方案
  4. spring aop提供了两种实现方式jdk和cglib
  5. 如何设置tomcat服务器编码为utf-8编码
  6. 在Dev GridControl中添加颜色可变的ProgressBar z
  7. HDU2256-Problem of Precision(矩阵构造+高速幂)
  8. SD卡操作相关的工具SDCardUtils
  9. 使用Lua 局部变量来优化性能,同一时候比較局部变量和全局变量
  10. c# 时间相关