![]() |
prog/01 のバックアップ(No.2) |
#include <stdio.h> #define N 10000 int main() { int i, j, k, c[5]; double x0, y0, x1, y1; for (k = 0; k < 5; k++) c[k] = 0; for (j = 0; j < N; j++) { y0 = (double) j / N; y1 = (double) (j + 1) / N; for (i = 0; i < N; i++) { x0 = (double) i / N; x1 = (double) (i + 1) / N; k = 0; if (x0 * x0 + y0 * y0 <= 1.0) k++; if (x1 * x1 + y0 * y0 <= 1.0) k++; if (x0 * x0 + y1 * y1 <= 1.0) k++; if (x1 * x1 + y1 * y1 <= 1.0) k++; c[k]++; } } double pi0 = 4.0 * c[4] / ((double) N*N); double pi1 = 4.0 * (c[1] + c[2] + c[3] + c[4]) / ((double) N*N); printf("pi=%.8f\n", (pi0 + pi1) / 2); return 0; }
#include <stdio.h> #define N 10000 int main() { int i, j, c = 0; double x0, y0, x1, y1; for (j = 0; j < N; j++) { y0 = (double) j / N; y1 = (double) (j + 1) / N; for (i = 0; i < N; i++) { x0 = (double) i / N; x1 = (double) (i + 1) / N; if (x0 * x0 + y0 * y0 <= 1.0) c++; if (x1 * x1 + y1 * y1 <= 1.0) c++; } } double pi = 2.0 * c / ((double) N*N); printf("pi=%.8f\n", pi); return 0; }
#include <stdio.h> #define N 10000 int main() { int i, j, c = 0; double x, y; for (j = 1; j < N; j++) { y = (double) j / N; for (i = 1; i < N; i++) { x = (double) i / N; if (x * x + y * y <= 1.0) c++; } } c *= 2; c += 2 * N - 1; double pi = 2.0 * c / ((double) N*N); printf("pi=%.8f\n", pi); return 0; }
#include <stdio.h> #define N 10000 int main() { int i, j, c = 0; for (j = 1; j < N; j++) { for (i = 1; i < N; i++) { if (i * i + j * j <= N * N) c++; } } printf("pi=%d\n", c * 4 + 4 * N - 2); return 0; }
#include <stdio.h> #define N 10000 int main() { int i = N - 1, j, c = 0; for (j = 1; j < N; j++) { while (i * i + j * j > N * N) i--; c += i; } printf("pi=%d\n", c * 4 + 4 * N - 2); return 0; }
コメント | お名前 | NameLink | |