UVA 10341 Solve It 解方程 二分查找+精度

题意:给出一个式子以及里面的常量,求出范围为[0,1]的解,精度要求为小数点后4为。

二分暴力查找即可。

e^(-n)可以用math.h里面的exp(-n)表示。

代码:(uva该题我老是出现Submission Error,过几天再试看看)

 

 /*
 *   Author:        illuz <iilluzen@gmail.com>
 *   Blog:          http://blog.csdn.net/hcbbt
 *   File:          uva10241.cpp
 *   Lauguage:      C/C++
 *   Create Date:   2013-08-25 15:37:46
 *   Descripton:    UVA 10341 Solve It,  bisection
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repu(i, a, b) for (int i = (a); i < (b); i++)
#define repf(i, a, b) for (int i = (a); i <= (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define swap(a, b) {int t = a; a = b; b = t;}
#define mc(a) memset(a, 0, sizeof(a))
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(x) ((x) * (x))
#define FI(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
typedef long long LL;
typedef unsigned long long ULL;

/****** TEMPLATE ENDS ******/

double p, q, r, s, t, u;

#define calc(x) (p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u)

int main() {
	while (scanf("%lf%lf%lf%lf%lf%lf", &p, &q, &r, &s, &t, &u)) {
		if (calc(0) < 0 || calc(1) > 0) printf("No solution\n");
		else {
			double x1 = 0, x2 = 1;
			while (abs(x1 - x2) >= 1e-10) {
				double x = (x1 + x2) / 2.0;
				if (calc(x) > 0) x1 = x;
				else x2 = x;
			}
			printf("%.4lf\n", x1);
		}
	}
	return 0;
}


 

 

posted @ 2013-08-26 19:31  pangbangb  阅读(274)  评论(0编辑  收藏  举报