(hdu step 4.1.1)Can you solve this equation?(使用二分法来求解方程组的解)

题目:

Can you solve this equation?

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

Problem Description

Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.


Input

The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);


Output

For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.


Sample Input

2
100
-4


Sample Output

1.6152
No solution!


Author

Redow

 

Recommend

lcy

题目分析:

简单题。是用二分法来求方程组的解。使用二分法来求方程组的解的前提是该方程具有单调性。

代码如下:

/*
 * a.cpp
 *
 *  Created on: 2015年2月16日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const double delta = 1e-6;//1e10表示10^10

double function(double x){
	return 8*pow(x,4.0) + 7*pow(x,3) + 2*pow(x,2) + 3*x + 6;
}

/**
 * 用二分法来求解方程组的解
 * y:方程组右边的数
 */
double work(double y){
	double mid;
	double left = 0;
	double right = 100;

	while(right - left > delta){//如果右边的索引还比左边的索引大
		mid = (left+right)/2;//计算中间值

		if(function(mid) < y){//如果y大于中间值...注意这种写法
			left = mid+(1e-7);//移动左边的索引。需要注意的是这里使用的偏移量应该比上面的小
		}else{//否则
			right = mid-(1e-7);//移动右边的索引
		}
	}

	return (left+right)/2;//返回最后的值
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		double y;
//		scanf("%d",&y);

		cin >> y;

		if(function(0) <= y && y <= function(100)){
			printf("%.4lf\n",work(y));
//			printf("%.4lf\n",find(y));
		}else{
			printf("No solution!\n");
		}
	}

	return 0;
}
时间: 2024-10-06 12:07:21

(hdu step 4.1.1)Can you solve this equation?(使用二分法来求解方程组的解)的相关文章

hdu 2199~Can you solve the equation?~二分法求解

Can you solve this equation? Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 28   Accepted Submission(s) : 17 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can y

hdu 2199 Can you solve this equation?(二分法求多项式解)

题意 给Y值,找到多项式 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y 在0到100之间的解. 思路 从0到100,多项式是单调的,故用二分法求解. 代码 double calc(double x){ return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6; } int main(){ int T; cin>>T; while(T--){ double Y; cin>>Y; double L,R; L = 0.0, R= 100.0;

hdu 2199 Can you solve this equation?(高精度二分)

http://acm.hdu.edu.cn/howproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13468    Accepted Submission(s): 6006 Problem Description Now,given the

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

(hdu step 1.3.1)FatMouse&#39; Trade(在收入需要一定的付出的情况下求最大收入)

题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5092 Accepted Submission(s): 1530   Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua

hdu 2199 Can you solve this equation?

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 题目大意:找到满足8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y的x值,注意精确度问题. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 5 using namespace std; 6 7 8 double fun(double s) 9 { 10 return

(hdu step 6.1.2)Eddy&#39;s picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)

题目: Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 172 Accepted Submission(s): 126   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be

(hdu step 5.1.1)A Bug&#39;s Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)

题目: A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 723 Accepted Submission(s): 277   Problem Description Background Professor Hopper is researching the sexual behavior of a rare spe

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble