C/C++の宿題を片付けます 86代目
■ このスレッドは過去ログ倉庫に格納されています
0744デフォルトの名無しさん
2007/05/07(月) 15:03:14#include <stdio.h>
#include <math.h>
int my_rand(int a, int b, int c, int d, int rn){
int temp = a*(rn%c) - d*(rn/c);
return temp > 0 ? temp : temp + b;
}
int main(){
double x, y, pi[10], ave = 0;
int rn = 1, a=16807, b=2147483647 , c=127773, d=2836, j, i, i_max = 20000, count_in;
for(j = 0; j < sizeof(pi)/sizeof(pi[0]); j++){
count_in = 0;
for(i = 0; i < i_max; i++){
rn = my_rand(a, b, c, d, rn);
x = (double)rn/b;
rn = my_rand(a, b, c, d, rn);
y = (double)rn/b;
if(sqrt(pow(x, 2) + pow(y, 2)) <= 1) count_in++;
}
pi[j] = 4*(double)count_in/i_max;
}
for(j = 0; j < sizeof(pi)/sizeof(pi[0]); j++) ave += pi[j];
ave /= sizeof(pi)/sizeof(pi[0]);
printf("pi = %f\n", ave);
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています