PAT基础编程题目-7-18 二分法求多项式单根

PAT基础编程题目-7-18 二分法求多项式单根

题目详情


题目地址:https://pintia.cn/problem-sets/14/problems/798

解答

C语言版

#include<stdio.h>
int main() {
   
	float a0, a1, a2, a3, a, b;
	double f, fa, fb;
	scanf("%f %f %f %f", &a3, &a2, &a1, &a0);
	scanf("%f %f", &a, &b);
	// 阈值取0.01
	while ((b - a) >= 0.01) {
   
		fa = a3 * a * a * a + a2 * a * a + a1 * a + a0;
		fb = a3 * b * b * b + a2 * b * b + a1 * b + a0;
		if (fa == 0)  // 区间端点是根
		{
   
			printf("%.2f", a);
			return 0;
		}
		if (fb == 0)
		{
   
			printf("%.2f", b);
			return 0;
		}
		if (fa * fb < 0)
		{
   
			f = a3 * ((a + b) / 2.0) * ((a + b) / 2.0) * ((a + b) / 2.0) + a2 * ((a + b) / 2.0) * ((a + b) / 2.0) + a1 * ((a + b) / 2.0) + a0;
			if (f == 0)
				break;
			else if (f * fa > 0) 
				a = (a + b) / 2.0;
			else if (f * fb > 0) 
				b = (a + b) / 2.0;
		}
	}
	printf("%.2f", (a + b) / 2.0);
	return 0;
}

C++版

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
   
	float a0, a1, a2, a3, a, b;
	double f, fa, fb;
	cin >> a3 >> a2 >> a1 >> a0;
	cin >> a >> b;
	// 阈值取0.01
	while ((b - a) >= 0.01) {
   
		fa = a3 * a * a * a + a2 * a * a + a1 * a + a0;
		fb = a3 * b * b * b + a2 * b * b + a1 * b + a0;
		if (fa == 0)  // 区间端点是根
		{
   
			cout << fixed << setprecision(2) << a;
			return 0;
		}
		if (fb == 0)
		{
   
			cout << fixed << setprecision(2) << b;
			return 0;
		}
		if (fa * fb < 0)
		{
   
			f = a3 * ((a + b) / 2.0) * ((a + b) / 2.0) * ((a + b) / 2.0) + a2 * ((a + b) / 2.0) * ((a + b) / 2.0) + a1 * ((a + b) / 2.0) + a0;
			if (f == 0)
				break;
			else if (f * fa > 0)
				a = (a + b) / 2.0;
			else if (f * fb > 0)
				b = (a + b) / 2.0;
		}
	}
	cout << fixed << setprecision(2) << (a + b) / 2.0;
	return 0;
}

Java版

import java.text.DecimalFormat;
import java.util.Scanner;
public class Main{
   

	public static void main(String[] args) {
   
		float a0 = 0, a1 = 0, a2 = 0, a3 = 0, a = 0, b = 0;
		double f, fa, fb;
		DecimalFormat decimalFormat = new DecimalFormat("0.00");
		Scanner scanner = new Scanner(System.in);
		if (scanner.hasNext()) {
   
			a3 = scanner.nextFloat();
			a2 = scanner.nextFloat();
			a1 = scanner.nextFloat();
			a0 = scanner.nextFloat();
			a = scanner.nextFloat();
			b = scanner.nextFloat();
		}
		scanner.close();
		// 阈值取0.01
		while ((b - a) >= 0.01) {
   
			fa = a3 * a * a * a + a2 * a * a + a1 * a + a0;
			fb = a3 * b * b * b + a2 * b * b + a1 * b + a0;
			if (fa == 0)  // 区间端点是根
			{
   
				System.out.println(decimalFormat.format(a));
				return;
			}
			if (fb == 0)
			{
   
				System.out.println(decimalFormat.format(b));
				return;
			}
			if (fa * fb < 0)
			{
   
				f = a3 * ((a + b) / 2.0) * ((a + b) / 2.0) * ((a + b) / 2.0) + a2 * ((a + b) / 2.0) * ((a + b) / 2.0) + a1 * ((a + b) / 2.0) + a0;
				if (f == 0)
					break;
				else if (f * fa > 0)
					a = (float) ((a + b) / 2.0);
				else if (f * fb > 0)
					b = (float) ((a + b) / 2.0);
			}
		}
		System.out.println(decimalFormat.format((a + b) / 2.0));
	}

}

创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!

全部评论

相关推荐

投递大华股份等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务