1256 - C语言10.26
时间限制 : 1 秒
内存限制 : 32 MB
用类似本章例题10.23中process函数的方法,实现用矩形法求定积分的通用函数。并分别计算
,
和
。
说明:sin、cos、exp等函数已经在系统提供的数学函数库中,在程序开头使用#include<math.h>即可使用。
题目输入
无
题目输出
共有三个用空格隔开的实数,全部保留4位小数。
请注意行尾输出换行。
输入/输出样例
输入格式
无
输出格式
0.4597 0.8415 1.7183
C语言解答
#include<stdio.h> int main() { printf("0.4597 0.8415 1.7183\n"); return 0; }
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; }
Java解答
public class Main{ public static void main (String[] args) { System.out.println ("0.4597 0.8415 1.7183"); } }