| 5 | 1/1 | 返回列表 |
| 查看: 1115 | 回復(fù): 6 | ||
| 當(dāng)前只顯示滿足指定條件的回帖,點(diǎn)擊這里查看本話題的所有回帖 | ||
836449366金蟲 (小有名氣)
|
[求助]
計算圓周率的小程序出錯 已有1人參與
|
|
|
我的程序是計算圓周率,原理是:在一個1*1的正方形及其內(nèi)切圓(圓心為(0.5,0.5))中,隨機(jī)數(shù)x和隨機(jī)數(shù)y組成的點(diǎn)位于正方形內(nèi),但要判斷這個點(diǎn)是否也在圓內(nèi),當(dāng)點(diǎn)數(shù)很多時,圓內(nèi)的點(diǎn)數(shù)除以總數(shù)再乘以4就等于圓周率了(即面積之比),我所編寫的程序如下: program main implicit none integer*4 :: i,j,n,ii common i real*4 :: r call random_number(x) call random_number(y) real*4,dimension(i):: x,y r=sqrt((x-0.5)**2+(y-0.5)**2) ii=0 do n=2,8,1 i=10**n do j=1,i if ( r .le. 0.5 ) then ii=ii+1 endif enddo enddo write(*,"(a,3F6.3)" "pai=",4*ii/i,x,yend 求蟲友幫幫忙,謝謝 |
金蟲 (小有名氣)
|
感覺你代碼的錯誤蠻多,然后我改寫了一下。 program main implicit none integer*4 :: num,i,j,ii real*4 :: r real*4,allocatable :: x( ,y(![]() num = 100000 allocate(x(num)) allocate(y(num)) call random_seed() call random_number(x) call random_number(y) ii = 0 do i = 1,num r = sqrt((x(i)-0.5)**2 + (y(i)-0.5)**2) if(r .le. 0.5) then ii = ii+1 end if end do write(*,"(a,F6.3)" "pai=",4.0*ii/numdeallocate(x,y) stop end program |
金蟲 (小有名氣)
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 265求調(diào)劑11408 +3 | 劉小鹿lu 2026-03-27 | 3/150 |
|
|---|---|---|---|---|
|
[考研] 321求調(diào)劑 +4 | 璞玉~~ 2026-03-25 | 4/200 |
|
|
[考研] 315調(diào)劑 +4 | 0860求調(diào)劑 2026-03-26 | 5/250 |
|
|
[考研] 314求調(diào)劑 +3 | 溪云珂 2026-03-26 | 3/150 |
|
|
[考研] 304求調(diào)劑 +3 | 曼殊2266 2026-03-27 | 3/150 |
|
|
[考研] 275求調(diào)劑 +10 | Micky11223 2026-03-25 | 12/600 |
|
|
[考研] 343求調(diào)劑 +4 | 贈我一本書 2026-03-23 | 4/200 |
|
|
[考研] 一志愿鄭州大學(xué),080500學(xué)碩,總分317分求調(diào)劑 +4 | 舉個栗子oi 2026-03-24 | 5/250 |
|
|
[考研] 071000生物學(xué)求調(diào)劑,初試成績343 +6 | 小小甜面團(tuán) 2026-03-25 | 6/300 |
|
|
[考研] 一志愿華理,數(shù)一英一285求A區(qū)調(diào)劑 +8 | AZMK 2026-03-25 | 10/500 |
|
|
[考研] 求調(diào)劑 +5 | 蘆lty 2026-03-25 | 6/300 |
|
|
[考研]
|
WWW西西弗斯 2026-03-24 | 8/400 |
|
|
[考研] 332求調(diào)劑 +6 | 032500 2026-03-25 | 6/300 |
|
|
[考研] 一志愿南航 335分 | 0856材料化工 | GPA 4.07 | 有科研經(jīng)歷 +6 | cccchenso 2026-03-23 | 6/300 |
|
|
[考研] 求調(diào)劑 +3 | QiMing7 2026-03-25 | 3/150 |
|
|
[考研] 各位老師您好:本人初試372分 +5 | jj涌77 2026-03-25 | 6/300 |
|
|
[考研] 347求調(diào)劑 +4 | L when 2026-03-25 | 4/200 |
|
|
[考研] 384求調(diào)劑 +3 | 子系博 2026-03-22 | 6/300 |
|
|
[考研] 333求調(diào)劑 +3 | ALULU4408 2026-03-23 | 3/150 |
|
|
[考研] 一志愿北京化工大學(xué)070300 學(xué)碩336求調(diào)劑 +5 | vv迷 2026-03-21 | 8/400 |
|