精華區beta C_Chat 關於我們 聯絡資訊
※ 引述《fragmentwing (片翼碎夢)》之銘言: : 原本想說要用log去算,發現超不準,只好直接算了 : 怕超過2*32次,就用10次和0.1的機率去算了 : 至少一次的機率==一次+兩次+...+N(抽的總次數)次 : 以下程式碼,才正式學C不到10天,請鞭小力點 : : #include<stdio.h> : #include<math.h> : int cntnis(int a,int b){ : int top=1,bottom=1; : for(int i=1;i<=b;i++){ : top*=(a-i+1); : bottom*=i; : } : : int compute=top/bottom; : return compute; : } : : double multi(double have,int havet,int nohavet){ : double result=pow(have,havet)*pow((1.0-have),nohavet); : return result; : } : : int main(){ : int times,cntn,i; : double haveis,mulh,probability,expectation=0; : : times=10; : haveis=0.1; : : for(i=1;i<=times;i++){ : cntn=cntnis(times,i); : mulh=multi(haveis,i,times-i); : probability=cntn*mulh; : printf("抽 %d 次中,抽中 %d 張的機率= %lf\n",times,i,probability); : expectation+=probability*i; : } : : printf("抽 %d 次,機率 %.3lf 期望值為: %lf",times,haveis,expectation); : : return 0; : } 用Python的話,你的原程式碼按照原有邏輯可以改寫成下面這樣 ---- import scipy.special times = 10 haveis = .1 expectation = 0 for i in range(1,times+1): probability = scipy.special.comb(times,i)*haveis**i*(1-haveis)**(times-i) print("抽" +str(times) +"次中,抽中"+ str(i)+"張的機率="+ str(probability)) expectation += probability*i print("抽"+str(times)+"次,機率 "+str(haveis)+" 期望值為"+str(expectation)) ---- : → hinanaitenco: 這用matlab吧 06/03 23:41 我不太建議 MATLAB很貴,而且你一使用很容易變成MATLAB的禁臠 -- 「ZZZZZ.....zzzzz.....」 ~中野二乃 https://i.imgur.com/kaUuF3R.jpg -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 71.198.27.180 (美國) ※ 文章網址: https://www.ptt.cc/bbs/C_Chat/M.1559585203.A.C97.html
leopika: 理工大學都有買吧? 06/04 02:07
是啊 所以理工大學有很多MATLAB的禁臠啊
dklash: 不是齁 人家剛學C你不要擾亂人家啦= = 06/04 02:09
dklash: 是說我很討厭用matlab 用起來感覺很討厭 06/04 02:10
我反而覺得MATLAB就是用起來太舒服才恐怖
dklash: 就是感覺用久了會變智障 06/04 02:12
attacksoil: <-禁臠 想改學python 06/04 02:12
學Python不難。難的是你要怎麼克制自己想要去用MATLAB 這有點像本本裡面女主變成男主的形狀之後,不管時隔多久, 只要再度見面,看到男主的O棒一樣X水直流
dklash: 不過我也沒什麼機會要用到matlab 最常用的時候是大學去修 06/04 02:12
dklash: 通訊三小的課 老師都用matlab上 用裡面的模擬工具 06/04 02:13
那個是Simulink。 MATLAB 的騙課神器
dklash: 會去修也是因為通訊系的朋友跟我說那老師超涼 06/04 02:13
dklash: 對!! 就是那個名字XD 06/04 02:16
dklash: 聽說那個很神 06/04 02:17
就很方便啊。其實也別說Simulink,光講繪圖library好了, matplotlib 跟 MATLAB的繪圖函式庫和介面相比真的超難用
fragmentwing: C已經很好了 我之前學的古早語言Fortran 宣告和執行 06/04 02:21
fragmentwing: 區是硬性規定要隔開的 06/04 02:21
fragmentwing: 怕變成python的禁臠 06/04 02:21
但Python不用錢啊。 MATLAB 一套只有基本函式庫+Simulink 就要 2150 USD了, 要持續更新的話你就要持續地繳交年費
fragmentwing: 不過fortran的goto因為是用行代碼,比C還要靈活 06/04 02:23
fragmentwing: 是怕先學python到時候會沒耐心學C 06/04 02:25
dklash: 看是為了甚麼學程式語言吧 06/04 02:25
fragmentwing: 為了愉悅(? 06/04 02:26
dklash: 確定自己永遠不用去管底層的東西的話 不學C也沒差 06/04 02:26
dklash: 反正那些事情問題通通讓編譯器和OS幫忙解決 06/04 02:27
dklash: 不用考慮那些用python就可以用得很開心了 06/04 02:27
aikotoba: 程式語言先學最順手的吧 肯寫肯動腦比較重要 06/04 02:35
Vulpix: 可以用Scilab啊,也是很不錯的。 06/04 02:36
orze04: python好學易上手沒錯 可是看一些初學者寫的爛CODE會發火 06/04 03:50
※ 編輯: arrenwu (71.198.27.180 美國), 06/04/2019 07:15:19
emptie: 爛code哪有分語言的 06/04 07:36
MoneyBlue: matlab的程式異常人性化,太好用惹,用完再用別的就會g 06/04 08:33
MoneyBlue: g 06/04 08:33
laechan: matlab 太久沒看, 會忘得一乾二淨哈.. 06/04 11:47