作者janewinnie (逃離)
看板Fortran
標題Re: [問題] 20個人中隨機抽取10個人
時間Fri Apr 16 19:10:28 2010
忘了回來板上看@@
我已經寫出一個方法了
先給每個人一個亂數
如果100人要取20人,就取亂數最大的20人
main program
implicit none
integer,parameter::k=20,n=100 !k是要選出的人數,n為總人數
integer::who(n) !who作為有無選上的標記
call choose(k,n,who)
write(*,*) who !1表是選上,0表示未選上
stop
end program
subroutine choose(k,n,who)
implicit none
integer::thft,i,n,person,loca(1)
real::rand(n)
integer::who(n)
call random_number(rand) !叫出"n"個亂數(每個人配一個亂數)
do i=1,k,1
loca=maxloc(rand) !找擁有最大亂數的那個人
person=loca(1)
who(person)=0 !這個人的who=1
rand(person)=0 !將這個人的亂數設為0,下次就不會再選到
enddo
return
end
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.123.185.83
1F:推 latinboy: 可以用 但是效率很驚人..... 04/16 20:54