| 7 | 1/1 | 返回列表 |
| 查看: 1111 | 回復(fù): 6 | ||
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 |
金蟲 (小有名氣)
金蟲 (小有名氣)
金蟲 (小有名氣)
| 7 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 材料與化工304求B區(qū)調(diào)劑 +3 | 邱gl 2026-03-26 | 5/250 |
|
|---|---|---|---|---|
|
[考研] 284求調(diào)劑 +4 | junqihahaha 2026-03-26 | 4/200 |
|
|
[考研] 打過很多競賽,085406控制工程300分,求調(diào)劑 +3 | askeladz 2026-03-26 | 3/150 |
|
|
[考研] 321求調(diào)劑 +3 | 璞玉~~ 2026-03-25 | 3/150 |
|
|
[考研]
|
然11 2026-03-19 | 6/300 |
|
|
[考研] 網(wǎng)絡(luò)空間安全0839招調(diào)劑 +4 | w320357296 2026-03-25 | 6/300 |
|
|
[考研] 347求調(diào)劑 +4 | L when 2026-03-25 | 4/200 |
|
|
[考研] 303求調(diào)劑 +6 | 元夕元 2026-03-20 | 7/350 |
|
|
[考研] B區(qū)考研調(diào)劑 +4 | yqdszhdap- 2026-03-22 | 5/250 |
|
|
[有機(jī)交流]
20+3
|
FENGSHUJEI 2026-03-23 | 5/250 |
|
|
[考研] 335求調(diào)劑 +4 | yuyu宇 2026-03-23 | 5/250 |
|
|
[考研] 一志愿國科過程所081700,274求調(diào)劑 +3 | 三水研0水立方 2026-03-23 | 3/150 |
|
|
[考研] 材料/農(nóng)業(yè)專業(yè),07/08開頭均可,過線就行 +3 | 呵唔哦豁 2026-03-23 | 4/200 |
|
|
[考研] 化學(xué)308分求調(diào)劑 +3 | 你好明天你好 2026-03-23 | 3/150 |
|
|
[考研] 350求調(diào)劑 +6 | weudhdk 2026-03-19 | 6/300 |
|
|
[考研] 求老師收我 +3 | zzh16938784 2026-03-23 | 3/150 |
|
|
[考研] 311求調(diào)劑 +3 | 26研0 2026-03-20 | 3/150 |
|
|
[考研] 303求調(diào)劑 +5 | 安憶靈 2026-03-22 | 6/300 |
|
|
[考研] 332求調(diào)劑 +3 | 鳳凰院丁真 2026-03-20 | 3/150 |
|
|
[考研] 材料學(xué)碩318求調(diào)劑 +5 | February_Feb 2026-03-19 | 5/250 |
|