Donghua oj- basic questions 85 questions

85 value polynomial calculated

Author: Turbo Time limit: 1S Section: cycling

Problem Description :

When the calculated value and outputs the following polynomial x <0.97, the absolute value is smaller than the threshold is reached until the last one (the polynomials are not included in the results).

image.png enter a description:

Input test data may be a plurality of sets, each row, each including two real numbers, the first of X (0.2≤x
<0.97), as a second threshold (≥0.000001), separated by a space.

Output Description:

For each test, an output line, the calculated result, to retain six decimal places. There was no space before and after the result output. Is a blank line between the two operation results. Entry:
0.2 0.000001
0.21 0.000001 Output Example:
1.095445

1.100000

Code:

/*
	T85 计算多项式的值 
*/ 

#include<stdio.h> 
#include<math.h>

double frac(double);
double getNum(double);
double getXpower(double);
 
int main() {
	double x = 0, threshold = 0, n = 1;
	double sum = 1, item = 0;
	int i = 0;
	
	while (scanf("%lf %lf", &x, &threshold) != EOF) {
		if (threshold > 1) {// 考虑特殊情况 
			printf("%lf\n\n", 0);
			continue;
		}
		
		n = 1;
		sum = 1;
		while (1) {
			item = (getNum(n - 1) / frac(n)) * pow(x, n);// 每一项的值 
			if (fabs(item) < threshold) {
				printf("%lf\n\n", sum);
				break;
			}
			sum += item;
			n++; 
		}
	}
	
	return 0;
}

// 阶乘 
double frac(double n) {
	if (n == 1 || n == 0) 
		return 1;
	return n * frac(n - 1); 
}

// 计算分子
double getNum(double n) {
	if (n == 0) {
		return 0.5;
	}
	
	return (0.5 - n) * getNum(n - 1);
}
Published 30 original articles · won praise 6 · views 5997

Guess you like

Origin blog.csdn.net/qq_41409120/article/details/104094375