东大OJ-一元三次方程解的个数

1043: Fixed Point

时间限制: 5 Sec  内存限制: 128 MB
提交: 26  解决: 5
[提交][状态][讨论版]

题目描述

In mathematics, a fixed point (sometimes shortened to fixpoint, also known as an invariant point) of a function is a point that is mapped to itself by the function. That is to say, x is a fixed point of the function f if and only if f(x) = x. For example, if f is defined on the real numbers by f(x)=x*x-3*x+4.,then 2 is a fixed point of f, because f(2) = 2.

Not all functions have fixed points: for example, if f is a function defined on the real numbers as f(x) = x + 1, then it has no fixed points, since x is never equal to x + 1 for any real number. In graphical terms, a fixed point means the point (x, f(x)) is on the line y = x, or in other words the graph of f has a point in common with that line. The example f(x) = x + 1 is a case where the graph and the line are a pair of parallel lines.

------ http://en.wikipedia.org/wiki/Fixed_point_%28mathematics%29

Our problem is,for a defined on real number function:

f(x)=a*x*x*x+b*x*x+c*x+d,how many different fixed points does it have?    

输入

There are multiple test cases.For each test cases, there are four integers a, b, c and d in a single line.
You can assume that -213<=a<=213, -213<=b<=213, -213<=c<=213, -213<=d<=213,and the number of the result is countable.

输出

For each test case, output the answer in a single line.

样例输入

3 111 793 -3456
5 -135 811 0
-1 9 10 -81

样例输出

3
3
3


#include<iostream>
#include<math.h>
using namespace std;
int main(){
	freopen("in.txt", "r", stdin);
	double a3, b3, c3, d3;
	double a2, b2, c2;
	while (cin >> a3 >> b3 >> c3 >> d3){
		c3--;
		a2 = 3 * a3; b2 = 2 * b3; c2 = c3;
		if (a3 == 0){
			if (b3 == 0)//一次方程
			{
				if (c3 == 0){//只剩常数
					cout << 0 << endl;
				}
				else cout << 1 << endl;
			}
			else{//二次方程
				double delta = c3*c3 - 4 * b3*d3;
				if (delta == 0)cout << 1 << endl;
				else if (delta>0)cout << 2 << endl;
				else cout << 0 << endl;
			}
		}
		else{//三次方程
			double delta = b2*b2 - 4 * a2*c2;
			if (delta <= 0)//单调递增
				cout << 1 << endl;
			else{
				double x1 = (-b2 - sqrt(delta)) / (2 * a2);
				double x2 = (-b2 + sqrt(delta)) / (2 * a2);
				double y1 = a3*x1*x1*x1 + b3*x1*x1 + c3*x1 + d3;
				double y2 = a3*x2*x2*x2 + b3*x2*x2 + c3*x2 + d3;
				double see = y1*y2;
				if (see == 0)cout << 2 << endl;
				else if (see > 0)cout << 1 << endl;
				else cout << 3 << endl;
			}
		}
	}
	return 0;
}


原文地址:https://www.cnblogs.com/weiyinfu/p/5013901.html