#include <stdio.h>
#include <math.h>
// 関数 y = sqrt(x)
double f(double x) {
}
int main() {
double a = 0.0, b = 1.0; // 積分区間 [0, 1]
int n; // 分割数
double true_value = 2.0 / 3.0; // 面積の真値 (解析解)
for (n = 2; n <= 100; n *= 2) { // 分割数を増やして誤差を調べる
double h = (b - a) / n; // 各台形の幅
double approx = 0.0;
// 台形近似法の計算
for (int i = 0; i < n; i++) {
double x1 = a + i * h;
double x2 = a + (i + 1) * h;
approx += (f(x1) + f(x2)) * h / 2.0;
}
// 誤差を計算
double error
= fabs(true_value
- approx
);
// 結果を出力
printf("%d\t%.6f\t%.6f\n", n
, approx
, error
); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CgovLyDplqLmlbAgeSA9IHNxcnQoeCkKZG91YmxlIGYoZG91YmxlIHgpIHsKICAgIHJldHVybiBzcXJ0KHgpOwp9CgppbnQgbWFpbigpIHsKICAgIGRvdWJsZSBhID0gMC4wLCBiID0gMS4wOyAvLyDnqY3liIbljLrplpMgWzAsIDFdCiAgICBpbnQgbjsgICAgICAgICAgICAgICAgICAvLyDliIblibLmlbAKICAgIGRvdWJsZSB0cnVlX3ZhbHVlID0gMi4wIC8gMy4wOyAvLyDpnaLnqY3jga7nnJ/lgKQgKOino+aekOinoykKICAgIAogICAgcHJpbnRmKCLliIblibLmlbBcdOi/keS8vOWApFx0XHToqqTlt65cbiIpOwogICAgZm9yIChuID0gMjsgbiA8PSAxMDA7IG4gKj0gMikgeyAvLyDliIblibLmlbDjgpLlopfjgoTjgZfjgaboqqTlt67jgpLoqr/jgbnjgosKICAgICAgICBkb3VibGUgaCA9IChiIC0gYSkgLyBuOyAgICAgLy8g5ZCE5Y+w5b2i44Gu5bmFCiAgICAgICAgZG91YmxlIGFwcHJveCA9IDAuMDsKCiAgICAgICAgLy8g5Y+w5b2i6L+R5Ly85rOV44Gu6KiI566XCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgZG91YmxlIHgxID0gYSArIGkgKiBoOwogICAgICAgICAgICBkb3VibGUgeDIgPSBhICsgKGkgKyAxKSAqIGg7CiAgICAgICAgICAgIGFwcHJveCArPSAoZih4MSkgKyBmKHgyKSkgKiBoIC8gMi4wOwogICAgICAgIH0KCiAgICAgICAgLy8g6Kqk5beu44KS6KiI566XCiAgICAgICAgZG91YmxlIGVycm9yID0gZmFicyh0cnVlX3ZhbHVlIC0gYXBwcm94KTsKCiAgICAgICAgLy8g57WQ5p6c44KS5Ye65YqbCiAgICAgICAgcHJpbnRmKCIlZFx0JS42Zlx0JS42ZlxuIiwgbiwgYXBwcm94LCBlcnJvcik7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K