Solution of "Openjudge" three-time equation _ two points

Source: Internet
Author: User

7891: One-yuan three-time equation solution
Total time limit: 1000ms memory limit: 65536kB description

Tangible as: Ax3+bx2+cx+d=0 such a unary three-time equation.

The coefficients of each of the equations (A,b,c,d are real numbers) are given, and it is agreed that there are three different real roots (the root range is between 100 and 100), and the absolute value of the difference between root and root is >=1. Requirements from small to large in the same row output the three real roots (between root and root space), and accurate to 2 digits after the decimal point. Enter a line that contains four real a,b,c,d, separated by a single space between the two adjacent numbers. Output row, contains three real numbers, for the equation of three real roots, in order from small to large, adjacent two numbers separated by a single space, accurate to the decimal point after 2 digits. Sample input

1.0-5.0-4.0 20.0
Sample output
-2.00 2.00 5.00

According to the topic description, the process of rooting is two points, as long as F (l) *f (R) <=0, there must be a root. Because there must be three roots, and two of the distance is greater than 1, so from left to right, sequentially scanned, two points.
Note: Because this topic uses floating-point, note that the function argument is a double type. I have been so WA for a long time.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;

#define MAXN
#define MAXM
#define INF 0x3f3f3f3f
#define LL long

double a,b,c,d;

Double f (double X)
{return
	a*x*x*x+b*x*x+c*x+d;
}

Double Dfs (double l,double r)
{
	if (r-l<=0.001) return l;
	
	Double mid= (l+r)/2;
	if (f (L) *f (mid) <=0 return DFS (L,MID);
	else return DFS (MID,R);
}

int main ()
{
	scanf ("%lf%lf%lf%lf", &a,&b,&c,&d);
	
	Double Ans[5];int cnt=0;
	for (int i=-100;i<100;++i)
	{
		double l=i,r=i+1;
		if (f (l) ==0)
			ans[++cnt]=l;
		else if (f (L) *f (R) <0)
			Ans[++cnt]=dfs (l,r);
		
		if (cnt>=3) break;
	
	printf ("%0.2lf%0.2lf%0.2lf\n", ans[1],ans[2],ans[3]);
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.