hdu2899 Strange fuction(二分兼三分)


好久沒做數學導數都想不過來了= =,本題是F''(x)恆大於0,所以F'(x)遞增,零點以前的F(x)都是減函數,所以以零點為判斷標准就行了。

而且事實證明無法用宏定義二變量函數。

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <iostream>
#include <stdlib.h>

using namespace std;

const int N = 100;
const int INF = 1000000;

double ans(double x,double y)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x;
}
double der(double x,double y)
{
return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;
}
int main()
{
// freopen("in.txt", "r", stdin);
int T;
double r, l, mid, y;
scanf("%d", &T);
while(T --)
{
scanf("%lf", &y);
r = 100;
l = 0;
while(fabs(r - l) > 1e-10)
{
mid = (r + l) / 2.0;//注意這里是+號,還有2.0
if(der(mid, y) > 0) r = mid;
else l = mid;
}
printf("%.4lf\n", ans(mid, y));
}
return 0;
}

下面是三分法,上圖吧:


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <iostream>
#include <stdlib.h>

using namespace std;

const int N = 100;
const int INF = 1000000;

double ans(double x,double y)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x;
}

int main()
{
// freopen("in.txt", "r", stdin);
int T;
double r, l, mid, y, midmid;
scanf("%d", &T);
while(T --)
{
scanf("%lf", &y);
r = 100;
l = 0;
while(fabs(r - l) > 1e-10)
{
mid = (r + l) / 2.0;
midmid = (mid + r) / 2.0;
if(ans(midmid, y) < ans(mid, y)) l = mid;
else r = midmid;
}
printf("%.4lf\n", ans(l, y));
}
return 0;
}



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: