zoukankan      html  css  js  c++  java
  • NYOJ 503(二分解方程)

     

    解方程

    时间限制:1000 ms | 内存限制:65535 KB
    难度:3
     
    描述

    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.

     
    输入
    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);
    输出
    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.
    样例输入
    2
    100
    -4
    样例输出
    2.0422
    No solution!

    exp(-10)=e^-10;
    1e-10=10^-10;因此用exp(-10)会wa

    #include<stdio.h>
    #include<math.h>
    double fun(double x)
    {
    
    	double y=8*pow(x,4)-7*pow(x,3)+2*pow(x,2)+3*x+6;
    	return y;
    }
    
    int main()
    {
    	int a,b,n,T;
    	double x1,x2,mid,m;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%lf",&m);
    		x1=0;
    		x2=100;
    		if(m<fun(0)||m>fun(100))/*值域符号相同则无解*/
    		{
    			printf("No solution!\n");
    			continue;
    		}
    		while((x2-x1)>1e-10)/*用exp(-10)就wa*/
    		{
    			mid=(x2+x1)/2.0;
    			if(fun(mid)<m)
    				x1=mid;
    			else 
    				x2=mid;
    		}
    		printf("%.4lf\n",mid);
    	}
    }  
    

      

  • 相关阅读:
    62. Unique Paths
    24. Swap Nodes in Pairs
    83. Remove Duplicates from Sorted List
    21. Merge Two Sorted Lists
    141. Linked List Cycle
    268. Missing Number
    191. Number of 1 Bits
    231. Power of Two
    9. Palindrome Number
    88. Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2517093.html
Copyright © 2011-2022 走看看