p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0
0<=p, r<=20 and -20<=q,s,t<=0
想法:
因為題目的係數有範圍限制,使得x越大,f(x)的值就越小,為遞減函數,因此可使用二分搜尋逼近答案。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cmath> | |
using namespace std; | |
#define F(x) (p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*pow(x,2) + u) | |
int main() | |
{ | |
int p, q, r, s, t, u; | |
while (scanf("%d %d %d %d %d %d",&p, &q, &r, &s, &t, &u)!=EOF) | |
{ | |
double Min=0.0, Max=1.0, mid; | |
for (int i=0; i<100; i++){ | |
mid = (Min+Max)/2; | |
if (F(mid)>0) Min = mid; | |
else Max = mid; | |
} | |
if (fabs(F(mid)-0) > 1e-10) printf ("No solution\n"); | |
else printf("%.4lf\n",mid); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言