国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 媒體動畫 > 正文

Director綜合技巧:用Lingo處理組合疑難

2024-09-08 19:26:56
字體:
來源:轉載
供稿:網友
用lingo解決組合問題
   原著:jerry mcmanus
   翻譯:alphachi

[問題]

   有5張卡片,從中任取3張,列出所有可能的結果。

[分析]

   輸入卡片列表并確定最終組合列表的長度 ——〉計算組合的總數并生成組合列表 ——〉輸出組合列表

[代碼]

   由于是有關排列組合的問題,必然會涉及到階乘的計算。為了方便起見,可以先設計一個階乘計算程序:

on mgetfactorial ( me , num)
   factorial = 1
repeat with x = num down to 1
     factorial = factorial * x
end repeat
return factorial
end

   接下來,就可以利用這個階乘計算程序得到組合的總數:

-- 計算階乘
listfactorial = me .mgetfactorial(plistcount)
subsetfactorial = me .mgetfactorial(psubsetcount)
listminussubsetfactorial = me .mgetfactorial(plistcount - psubsetcount)
-- 計算組合總數
ptotal = listfactorial / (subsetfactorial * (listminussubsetfactorial))
pnumleft = ptotal

   現在,借助一個索引數值,通過循環語句即可生成一個索引列表:

on mgetcombination ( me )
-- 檢測是否為第一次循環
if pnumleft = ptotal then
-- 是第一次循環,使用當前子列表
     pnumleft = pnumleft - 1
else
-- 不是第一次循環,獲取新的子列表
x = psubsetcount
-- 在當前子列表中循環并增值
repeat while pcurrentsubset[ x ] = plistcount - psubsetcount + x
x = x - 1
end repeat
     pcurrentsubset[ x ] = pcurrentsubset[ x ] + 1
repeat with y = ( x + 1 ) to psubsetcount
       pcurrentsubset[ y ] = pcurrentsubset[ x ] + y - x
end repeat
-- 獲取新的子列表
     pnumleft = pnumleft - 1
end if
end

   之所以沒有直接對實際的卡片列表進行直接操作,是為了讓程序擁有更強的適應性。因為只要擁有了索引列表,就可以對任何傳入的實際列表進行“組合”操作,而不僅僅限于這個卡片列表。當然,只需再添加一些代碼,即可生成實際的結果列表:

-- 生成結果列表
combination = []
repeat with x = 1 to psubsetcount
   combination. add (pitemlist[pcurrentsubset[ x ]])
end repeat

   下面的影片便是完成后的“組合生成器”:


播放,以上示例,需下載此插件。 點擊下載插件

[說明]

   這項技巧雖然比較簡單,但使用的范圍卻非常廣泛,例如卡片的隨機抽取或數列的隨機生成。此外,在許多涉及到需要列舉組合結果的數學問題中都占有一席之地。

相關附件

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延边| 汽车| 山丹县| 文化| 九龙县| 沁阳市| 衡水市| 略阳县| 丹巴县| 宾阳县| 中超| 阿勒泰市| 敦化市| 巴楚县| 镇赉县| 威信县| 南汇区| 祥云县| 朝阳市| 四平市| 榆林市| 锡林浩特市| 柳江县| 汽车| 瑞丽市| 天水市| 塔河县| 吴旗县| 成都市| 呼和浩特市| 寿宁县| 类乌齐县| 萨嘎县| 阿拉善左旗| 老河口市| 岗巴县| 宁城县| 东光县| 高陵县| 师宗县| 泰和县|