游客 Signup | Login
中文 | En

1256 - C语言10.26

用类似本章例题10.23中process函数的方法,实现用矩形法求定积分的通用函数。并分别计算

说明:sin、cos、exp等函数已经在系统提供的数学函数库中,在程序开头使用#include<math.h>即可使用。

Input

Output

共有三个用空格隔开的实数,全部保留4位小数。

请注意行尾输出换行。

Examples

Input

Output

0.4597 0.8415 1.7183

Solution C

#include<stdio.h>
int main()
{
	printf("0.4597 0.8415 1.7183\n");
	return 0;
}

Solution C++

#include <stdio.h>
#include <math.h>
int main() {
	double integrate(double low, double high, double (*func)(double));
	printf("%.4f %.4f %.4f\n", integrate(0.0, 1.0, sin), integrate(0.0, 1.0, cos), integrate(0.0, 1.0, exp));
	return 0;
}
/* 通用的矩形法求定积分的函数 */
double integrate(double low, double high, double (*func)(double)) {
	double temp, result = 0, multiply = 1.0, step = 0.000001;
	/* 当积分下限大于上限时,最终结果需要乘-1 */
	if (low > high) {
		temp = low; low = high; high = temp;
		multiply = -1.0;
	}
	/* 矩形法求定积分的过程 */
	while (low + step <= high) {
		result += (*func)(low + step * 0.5) * step;
		low += step;
	}
	result *= multiply;
	return result;
}

Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题